@ARGV 数组里保存着命令中给出的参数,除了程序本身的名字外 --- 它用 $0 来指示。
如下代码示例:
[code=perl]#!/usr/bin/perl
print "our executable file name is $0\n";
$i = 1;
foreach (@ARGV) {
print "arg$i is $_ \n";
$i++;
}[/mw_shl_code]
运行输出:./argv.pl hello perl world
our executable file name is ./argv.pl
arg1 is hello
arg2 is perl
arg3 is world
从 这里知道,如果程序中使用了钻石操作符,那么它会依次处理读入命令行中给出的文件中的内容。实际上,它也是通过读取 @ARGV 数组来获取命令行上给出的文件名。因此,在使用 <> 之前,我们可以对 @ARGV 动些手脚,考虑下面代码:
[Perl] 纯文本查看 复制代码 #!/usr/bin/perl
@ARGV = qw/ temp.txt temp2.txt /;
$i = 1;
foreach (@ARGV) {
print "arg$i is $_ \n";
$i++;
}
while (<>) {
chomp;
print "It was \"$_\" \n";
}
运行输出:./argv.pl hello world
arg1 is temp.txt
arg2 is temp2.txt
It was "hello world"
It was "hello perl"
It was "welcome"
It was "www.groad.net" |