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 陣列。