曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3663|回复: 0
打印 上一主题 下一主题

命令行参数与 @ARGV 数组

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-9-24 20:09:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
@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"
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 11:00 , Processed in 0.080535 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表