--> 本章目的:通过使用命令行参数以及shell来与Perl进行交互.
关键词:特殊数组@ARGV Getopt::Std模块 Getopt::Long模块 创建Perl shell 将shell集成到perl中
1.解析命令行参数
1.1命令行约定
采用选项与值思想
>program -a 1 -b -2 -c -d
无值选项可捆绑
>program -a1 -b=2 -cd
GNU Long选项定义
>program --option1=1 --option2=-2 --option3 --option4
1.2 @ARGV数组
@ARGV数组包含有在程序启动后传递给程序的所有参数.
>perl myscript -u sventek -p cuckoo
scalar(@ARGV) 获得参数的个数
$#ARGV 获得参数数组的最大索引
#!/usr/bin/perl
#turntohash.pl
use warnings;
use strict;
my %args;
if (scalar(@ARGV)%2){
die "Odd number of argument passed to $0\n";
}
else{
%args=@ARGV;
foreach (keys %args){
die "Bad argument '$_' does not start with -\n" unless /^-/;
}
}
a.向Perl传递参数
Perl可以识别脚本名字
>perl -Mstrict -MMymodule -w myscript -u sventek -p suckoo
当脚本名类似与命令行选项时
>perl -Mstrict -MMyModule -w -- myscript -u sventek -p cuckoo
设置环境变量向Perl传递参数
export PERL5OPT="-Mstrict -MPOSIX -w"
b.设置@ARGV中的变量
c.从@ARGV中读取文件
在使用"<>"时,Perl将会试图以文件的方式依次打开@ARGV中的每个元素
简单的cat版本
perl -e "print while <>" file1 file2
使用-n或-p选项将隐含while(<>){...}循环代码
perl -ne 'print' file1.txt file2.txt
带行号的cat版本
#!/usr/bin/perl
#cat.pl
use warnings;
use strict;
print "$. :$_" while <>;
print "$ARGV:$.:$_" while <>;
切换文件时重置行数
#!/usr/bin/perl
#bettercat.pl
use warnings;
use strict;
while(<>){
print "$ARGV:$.:$_";
close (ARGV) if eof;
}
简单的grep
#!/usr/bin/perl
#simplegrep.pl
use warnings;
use strict;
die "Useage:$0 pattern filen1 [filne2] ...\n" unless scalar(@ARGV) >1;
my $pattern=shift;
while(<>){
print "$ARGV:$.:$_" if /$pattern/;
close(ARGV) if eof;
}
d.@ARGV与标准输入
如果无文件名,将标准输入添加到数组中
@ARGV=('-') unless @ARGV
>cat filename |simplegrep pattern -
>simplegrep pattern - <filename
1.3 利用Getopt::Std进行简单的命令行处理
简单定义几个选项
use Getopt::Std;
getopt("atef")
可以通过 $opt_X来访问选项的值,将X替换为选项的名称
如果使用了use strict或use strict vars需要用our预先声明变量.
另一种方法是向getopt提供第二个参数的hash引用,如:
my %opt;
getopt("atef",\%opt);
foreach (keys %opt){
print "\t$_ => $opt{$_}\n";
}
利用getopts更智能的处理参数
定义布尔选项与值选项并检查无效的选项,允许捆绑选项
getopts("aetf:");
这里的'aet'为布尔选项,'f'为值选项
1.4 利用Getopt::Long进行更为复杂的命令行处理
a. 简单的参数处理
定义两个布尔选项:verbose background
#!/usr/bin/perl -w
#definebool.pl
use strict;
use Getopt::Long;
my ($verbose,$background);
GetOptions (verbose => \$verbose,background => \$background);
print "Verbose messages on \n" if $verbose;
如果不提供引用
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
our $opt_verbose;
GetOptions ('verbose');
可以提供hash引用来接收参数
#!/usr/bin/perl -w
use strict;
use Getoption::Long;
my %opts;
GetOptions(\%opts,'verbose','background');
对于单个负号的参数
GetOptions(''=>\$read_from_stdio);
#turntohash.pl
use warnings;
use strict;
my %args;
if (scalar(@ARGV)%2){
die "Odd number of argument passed to $0\n";
}
else{
%args=@ARGV;
foreach (keys %args){
die "Bad argument '$_' does not start with -\n" unless /^-/;
}
}
a.向Perl传递参数
Perl可以识别脚本名字
>perl -Mstrict -MMymodule -w myscript -u sventek -p suckoo
当脚本名类似与命令行选项时
>perl -Mstrict -MMyModule -w -- myscript -u sventek -p cuckoo
设置环境变量向Perl传递参数
export PERL5OPT="-Mstrict -MPOSIX -w"
b.设置@ARGV中的变量
c.从@ARGV中读取文件
在使用"<>"时,Perl将会试图以文件的方式依次打开@ARGV中的每个元素
简单的cat版本
perl -e "print while <>" file1 file2
使用-n或-p选项将隐含while(<>){...}循环代码
perl -ne 'print' file1.txt file2.txt
带行号的cat版本
#!/usr/bin/perl
#cat.pl
use warnings;
use strict;
print "$. :$_" while <>;
print "$ARGV:$.:$_" while <>;
切换文件时重置行数
#!/usr/bin/perl
#bettercat.pl
use warnings;
use strict;
while(<>){
print "$ARGV:$.:$_";
close (ARGV) if eof;
}
简单的grep
#!/usr/bin/perl
#simplegrep.pl
use warnings;
use strict;
die "Useage:$0 pattern filen1 [filne2] ...\n" unless scalar(@ARGV) >1;
my $pattern=shift;
while(<>){
print "$ARGV:$.:$_" if /$pattern/;
close(ARGV) if eof;
}
d.@ARGV与标准输入
如果无文件名,将标准输入添加到数组中
@ARGV=('-') unless @ARGV
>cat filename |simplegrep pattern -
>simplegrep pattern - <filename
1.3 利用Getopt::Std进行简单的命令行处理
简单定义几个选项
use Getopt::Std;
getopt("atef")
可以通过 $opt_X来访问选项的值,将X替换为选项的名称
如果使用了use strict或use strict vars需要用our预先声明变量.
另一种方法是向getopt提供第二个参数的hash引用,如:
my %opt;
getopt("atef",\%opt);
foreach (keys %opt){
print "\t$_ => $opt{$_}\n";
}
利用getopts更智能的处理参数
定义布尔选项与值选项并检查无效的选项,允许捆绑选项
getopts("aetf:");
这里的'aet'为布尔选项,'f'为值选项
1.4 利用Getopt::Long进行更为复杂的命令行处理
a. 简单的参数处理
定义两个布尔选项:verbose background
#!/usr/bin/perl -w
#definebool.pl
use strict;
use Getopt::Long;
my ($verbose,$background);
GetOptions (verbose => \$verbose,background => \$background);
print "Verbose messages on \n" if $verbose;
如果不提供引用
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
our $opt_verbose;
GetOptions ('verbose');
可以提供hash引用来接收参数
#!/usr/bin/perl -w
use strict;
use Getoption::Long;
my %opts;
GetOptions(\%opts,'verbose','background');
对于单个负号的参数
GetOptions(''=>\$read_from_stdio);

发表评论