####################默认变量
默认变量$_与@_
$_ is known as the "default input and pattern matching space". In other words, if you read in from a file handle at the top of a while loop, or run a foreach loop and don't name a loop variable, $_ is set up for you. Then any regular expression matches, chops (and lcs and many more) without a parameter, and even prints assume you want to work on $_. Thus:
while ($line = <FH>) {
if ($line =~ /Perl/) {
print FHO $line;
}
print uc $line;
}
Shortens to:
while (<FH>) {
/Perl/ and
print FHO ;
print uc;
}
@_ is the list of incoming parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_[0], the second parameter as $_[1] and so on. And you can refer to $#_ as the index number of the last parameter:
#######
默认变量$_与@_
$_ is known as the "default input and pattern matching space". In other words, if you read in from a file handle at the top of a while loop, or run a foreach loop and don't name a loop variable, $_ is set up for you. Then any regular expression matches, chops (and lcs and many more) without a parameter, and even prints assume you want to work on $_. Thus:
while ($line = <FH>) {
if ($line =~ /Perl/) {
print FHO $line;
}
print uc $line;
}
Shortens to:
while (<FH>) {
/Perl/ and
print FHO ;
print uc;
}
@_ is the list of incoming parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_[0], the second parameter as $_[1] and so on. And you can refer to $#_ as the index number of the last parameter:
#######