Perl Getopt and GetOptions

官方资料: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

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值