Getopt::Std模块的使用

 Getopt::Std模块的使用:

初始设置:  

在程序中加入如下代码:
          use Getopt::Std;
               use vars qw($opt_d $opt_f $opt_p);
               getopts('d:f:p');
               (注意上两行的参数要对应)

[ 解释一下"d:f:p",d和f后有冒号,表示-d,-f后面要跟参数。p后面没有冒号,表示-p后面不带参数。
而且-d,-f后所跟的参数分别赋给变量$opt_d和$opt_f。对于变量$opt_p,若命令行加了-p,则$opt_p=1,否则为0]

举个例子就很清楚了:
vi file.pl

添加如下代码:
use Getopt::Std;
use vars qw($opt_d $opt_f $opt_p);
getopts('d:f:p');

print "/$opt_d =>; $opt_d/n" if $opt_d;
print "/$opt_f =>; $opt_f/n" if $opt_f;
print "/$opt_p =>; $opt_p/n" if $opt_p;

然后在命令行中运行:
perl file.pl -d 20040412 -f louiskoochen -p

可得到下列形式的输出:
$opt_d =>;200040412
$opt_f =>;louiskoochen
$opt_p =>;1

 

    use Getopt::Std;
    getopt('oDI');    # -o, -D & -I take arg.  Sets $opt_* as a side effect.
getopt('oDI', /%opts); # -o, -D & -I take arg. Values in %opts
getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
# Sets $opt_* as a side effect.
getopts('oif:', /%opts); # options as above. Values in %opts

DESCRIPTION

The getopt() function processes single-character switches with switch clustering. Pass one argument which is a string containing all switches that take an argument. For each switch found, sets $opt_x (where x is the switch name) to the value of the argument if an argument is expected, or 1 otherwise. Switches which take an argument don't care whether there is a space between the switch and the argument.

The getopts() function is similar, but you should pass to it the list of all switches to be recognized. If unspecified switches are found on the command-line, the user will be warned that an unknown option was given. The getopts() function returns true unless an invalid option was found.

Note that, if your code is running under the recommended use strict 'vars' pragma, you will need to declare these package variables with "our":

    our($opt_x, $opt_y);

For those of you who don't like additional global variables being created, getopt() and getopts() will also accept a hash reference as an optional second argument. Hash keys will be x (where x is the switch name) with key values the value of the argument or 1 if no argument is specified.

To allow programs to process arguments that look like switches, but aren't, both functions will stop processing switches when they see the argument -- . The -- will be removed from @ARGV.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 Perl 子程序,用于解析命令行参数。让我逐行解释一下这个子程序的功能: 1. 首先,它使用Getopt::Long 模块来处理命令行选项和参数。通过 `use Getopt::Long;` 引入了该模块。 2. `Getopt::Long::Configure("no_ignore_case");` 配置选项,使得对选项大小写不敏感。 3. 如果存在环境变量 `$ENV{PERLCC_OPTS}`,则将其拆分成单个字符并添加到 `@ARGV` 数组的开头。这是为了支持使用 Perlcc 编译器编译的脚本。 4. 如果没有传递任何参数,输出 "no parameter" 并调用 `helpme()` 子程序。 5. 创建一个哈希引用 `$Options`,用于存储解析后的选项和参数。 6. 使用 `Getopt::Long::Getoptions()` 解析命令行选项和参数,并将结果存储在 `$Options` 和其他变量中。具体的选项和参数定义如下: - "source=s":期望一个字符串作为 `-s` 或 `--source` 选项的值,并将其赋值给 `$src` 变量。 - "destiny=s":期望一个字符串作为 `-d` 或 `--destiny` 选项的值,并将其赋值给 `$des` 变量。 - "mem=s":期望一个字符串作为 `--mem` 选项的值,并将其赋值给 `$mem` 变量。 - "help|?":如果出现 `-h`、`--help` 或 `?` 选项,则将 `$help_flag` 标志设置为 1。 7. 将 `$Options->{v}` 的值强制转换为数字,以确保其为数值类型。 8. 如果 `$src` 的长度不为零,则继续执行后续操作;否则,输出错误信息 "Error: Missing source file: -s, --source" 并将 `$help_flag` 设置为 1。 9. 如果 `$help_flag` 为 1,调用 `helpme()` 子程序。 这个子程序的目的是解析命令行参数,并根据参数的值执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值