分类:perl 的日志列表

20075/02

perl的预参数处理

Published by nowa 18:29:14 二月 05th,2007 in perl.
今天写flickpr的时候查资料总结出来的:

1、可以使用Getopt::Std模块来实现。
Quotes
use Getopt::Std;
# -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o
getopts("vDo:");

# -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o}
getopts("vDo:", \%args);

2、或者使用Getopt::Long来允许命名参数:
Quotes
use Getopt::Long;
GetOptions( "verbose" => \$verbose, # --verbose
"Debug" => \$debug, # --Debug
"output=s" => \$output );

3、如果不想对模块有所依赖,那么我们可以自己实现:
Quotes
my $var_name = "";
foreach (@ARGV) {
$var_1=int($_) if $var_name eq "-i" && !/^-/;
$var_2=$_ if $var_name eq "-c" && !/^-/;
$var_name=$_ if /^-/;
}


ps:今天收到了有道的来信

Getopt  perl  

已被阅读402次 1 Comments 0 Trackbacks