"print $ ARGV"会以任何方式改变参数数组吗?

Mee*_*cus 3 perl shift argv command-line-arguments

这是一个例子:

$a = shift; 
$b = shift; 
push(@ARGV,$b); 
$c = <>; 

print "\$b: $b\n"; 
print "\$c: $c\n"; 
print "\$ARGV: $ARGV\n"; 
print "\@ARGV: @ARGV\n"; 
Run Code Online (Sandbox Code Playgroud)

并输出:

$b: file1 
$c: dir3 

$ARGV: file2 
@ARGV: file3 file1 
Run Code Online (Sandbox Code Playgroud)

我不明白在没有任何索引的情况下打印$ ARGV时究竟发生了什么.它是否打印第一个参数然后从数组中删除它?因为我认为在所有语句之后数组变成:

file2 file3 file1
Run Code Online (Sandbox Code Playgroud)

调用:

perl port.pl -axt file1 file2 file3 
Run Code Online (Sandbox Code Playgroud)

file1包含以下行:

dir1 
dir2 
Run Code Online (Sandbox Code Playgroud)

文件2:

dir3 
dir4 
dir5 
Run Code Online (Sandbox Code Playgroud)

文件3:

dir6 
dir7
Run Code Online (Sandbox Code Playgroud)

TLP*_*TLP 7

Greg引用了相应的文档,所以这里简要介绍了会发生什么

$a = shift;                 # "-axt" is removed from @ARGV and assigned to $a
$b = shift;                 # "file1" likewise
push(@ARGV,$b);             # "file1" inserted at end of @ARGV
$c = <>;                    # "file2" is removed from @ARGV, and its file
                            # handle opened, the first line of file2 is read
Run Code Online (Sandbox Code Playgroud)

当打开"file2"的文件句柄时,它会设置文件名$ARGV.正如格雷格所说,@ARGV并且$ARGV是完全不同的变量.

钻石操作员的内部工作<>可能让你感到困惑,因为它具有近似性$ARGV = shift @ARGV


Gre*_*ill 5

在Perl中,$ARGV@ARGV是完全不同的.来自perlvar:

$ ARGV

包含读取时当前文件的名称<>.

@ARGV

该数组@ARGV包含用于脚本的命令行参数.$#ARGV通常是参数的数量减去1,因为它$ARGV[0]是第一个参数,而不是程序的命令名本身.请参阅$0命令名称.