| ACADEMIC COMPUTING and COMMUNICATIONS CENTER | ||||||||
| ||||||||||||||||||||
Special Variables | ||||||||||||||||||||
| @ARGV | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @_ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| $_ | ||||||||||||||||||||
$_ is the default scalar argument for many operators.
For example:
foreach (@array) {
print if /A/;
}
is really the same as:
foreach $_ (@array) {
print $_ if ($_ =~ /A/);
}
So $_ is used most often when it isn't seen.
Some operators that use $_ as default
are foreach, grep, map, print, chop.
|
||||||||||||||||||||
| $/ | ||||||||||||||||||||
|
$/ is the string that separates lines when read with the <> operator. Remember that in scalar context, <F> reads one line from the filehandle F. What constitutes a line? Whatever ends with $/. By default this is a newline, but you can set $/ to anything. In fact, if you set it to undef, you will then slurp in the whole file in scalar mode. |
||||||||||||||||||||
| $! | ||||||||||||||||||||
|
$! is set to an error message if a preceeding opeartion fails. Often used with die to report errors.
open (F, "filename") or die "Can't open filename: $!";
|
||||||||||||||||||||
| $@ | ||||||||||||||||||||
|
$@ is set to a non-null error message if eval fails. Very useful for trapping exceptions.
eval {$some_code};
if ($@) {
print STDERR "eval failed: $@\n";
}
|
||||||||||||||||||||
| Others | ||||||||||||||||||||
|
There are all sorts of other special variables, such as $| that set output buffering, or $$ that contains the current pid. But the above are the most useful to be aware of. |
||||||||||||||||||||
| Perl II | Previous: 2. Sources | Next: 4. Subroutines |
| 1999-6-15 BobG |
|