官方资料:http://perldoc.perl.org/Getopt/Long.html,以下转载自:http://hi.baidu.com/juliehust/blog/item/386388f4e816d17fdcc4742a.html
正文:
&GetOptions("p=s" => \$ParameterFile,
"s:s@" => \@Step,
"notify!" => \$NotifyFlag,
);
$NotifyFlag=0 if (! defined $NotifyFlag);
我们在linux常常用到一个程序需要加入参数,现在了解一下perl中的有关控制参数的函数.getopt.在linux有的参数有二种形式.一种是–help,另一种是-h.也就是-和–的分别.–表示完整参数.-表示简化参数.
在perl中也分这二种.
Getopt::Std模块的功能: 初始化perl命令行中所接受的参数,简化了命令行参数的解析。
简化参数例子:
#!/usr/bin/perl -w
usestrict;useGetopt::Std;
usevars
qw($opt_a $opt_b $opt_c);
getopts('d:f:p:');
print "\$opt_a =>; $opt_a\n" if $opt_a;
print "\$opt_b =>; $opt_b\n" if $opt_b;
print "\$opt_c =>; $opt_c\n" if $opt_c;
输出如下:
[root@mail test]# ./getopt.pl -a aa -b bb -c cc
$opt_a =>; aa
$opt_b =>; bb $opt_c =>; cc
带值参数
※参数类型:整数, 浮点数, 字串
GetOptions( ‘tag=s’ => \$tag );
'='表示此参数一定要有参数值, 若改用 ':'代替表示参数不一定要有参数值
's’表示传递字串参数, 若为’i'表传递整数参数, 若为’f'表传递浮点数
example:
test.pl –tag=string
or
test.pl –tag string
多参数值的参数
GetOptions ("library=s" => \@libfiles);
参数传到 @tag
or
GetOptions ("library=s@" => \$libfiles);
参数传到 @$tag
example:
test.pl –library lib/stdlib –library lib/extlib