perl 中的特殊变量

1. $_:默认输入和模式搜索空间。

例 1:

my @array_variable = (1 2 3 4);
foreach (@array_variable){
    print $_."\n";    # $_ will get the value 1,2,3,4 in loop, if no other variable is supplied.
}

例 2:

while (<FH>){
    chomp($_);    # $_ refers to the iterating lines in the loop.
}

以下函数使用 $_ 作为默认参数:

abs, alarm, chomp, chop, chr, chroot, cos, defined, eval,
evalbytes, exp, fc, glob, hex, int, lc, lcfirst, length, log,
lstat, mkdir, oct, ord, pos, print, printf, quotemeta, readlink,
readpipe, ref, require, reverse (in scalar context only), rmdir,
say, sin, split (for its second argument), sqrt, stat, study,
uc, ucfirst, unlink, unpack.

2. @_:该数组包含传递给子程序的参数。

例 1:

example_sub( $test1, $test2, $test3 );

sub example_sub {
    my ( $test1, $test2, $test3 ) = @_; 
}

在子例程中,数组 @_ 包含传递给该子例程的参数。在子程序中,@_ 是数组运算符 popshiftdefault 数组。