Perl's special variables give you the ability to take greater control over how perl behaves and to streamline many operations. On the downside, the special variables can be difficult to learn and remember. Described below are some of the more important special variables and their uses. See the Perl documentation for information on the others.
$_
This special variable is used by default for input and pattern-matching. When no variable is specified in such an operation, this one is used. Alternatively, you can use it by specifying it by name.
Example:
while($input = <STDIN>){} # The variable $input is specified to receive
# the input.
while($_ = <STDIN>){} # The special variable $_ is specified to receive
# the input.
while(<STDIN>){} # No variable is specified. $_ receives the input.
if($content =~ /$pattern/){} # The variable is $content is compared
# to the pattern.
if($_ =~ /$pattern/){} # The special variable $_ is compared
# to the pattern.
if(/$pattern/){} # The special variable $_ is compared
# to the pattern by default.
@_
This is a special variable that is similar to $_ but it is an array containing a list. The individual elements can be accessed via subscripts like so: $_[0], $_[1], $_[2], etc.
Use of @_ in subroutines
A valuable use of @_ is holding the parameters passed to a subroutine. In our previous discussion of subroutines, operations were performed on global variables only. Passing values to a subroutine offers more flexibilty in deciding which values will be opearated on while the contents of global variables can be preserved.
Examples:
$sum = add($a,$b); #Pass to values and return the sum
sub add {
$a = $_[0];
$b = $_[1];
return $a + $b;
}
@my_list = ($a,$b); # swap two list elements
swap(@my_list);
sub swap{
$temp = $_[0];
$_[0] = $_[1];
$_[1] = $temp;
}
# print elements in a list that match a pattern
search_list_for_pattern($pattern, @my_list);
sub search_list_for_pattern{
$pattern = shift(@_);
foreach (@_){
if(/$pattern/){
print "$_ matches $pattern";
}
}
}
$a = 7;
$result = double($a); # Simple doubling function called on
# global variable $a.
sub double{
my $a = $_[0]; # Private variable $a is defined and initialized
# with the value of the global variable $a.
$a = 2 * $a; # Private $a is doubled.
# Value of global variable $a doesn't change.
return $a; # Value of private $a is returned
} # Private variable $a disappears as
# the enclosing block ends.
print $a . "\n";
print $result . "\n";
Output
7 14
@ARGV
The "Argument Vector" special array variable contains the parameters passed on the command-line for
the running of the currently executing perl script.
Eample:
perl my_script.pl -w inputfile outputfile
# $ARGV[0] = 'my_script.pl'
# $ARGV[1] = '-w'
# $ARGV[2] = 'inputfile'
# $ARGV[3] = 'outputfile'
$#
This special operator gives the last subscript of an array. It is equal to the number of elements in an array
minus one because array indexes start at zero.
@my_array = ("Huey","Dewey","Louie");
print $#my_array . "\n";
print $my_array[$#my_array] . "\n";
Output
2 Louie