Perl - Special Variables

There are some variables which have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_ ( explained below ).
Most of the special variables have an english like long name, e.g., Operating System Error variable $! can be written as $OS_ERROR. But if you are going to use english like names, then you would have to put one line use English; at the top of your program file. This guides the interpreter to pickup exact meaning of the variable.
The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines −
#!/usr/bin/perl

foreach ('hickory','dickory','doc') {
   print $_;
   print "\n";
}
When executed, this will produce the following result −
hickory
dickory
doc

Again, let's check the same example without using $_ variable explicitly −
#!/usr/bin/perl

foreach ('hickory','dickory','doc') {
   print;
   print "\n";
}
When executed, this will also produce the following result −
hickory
dickory
doc

The first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed in $_, and is used by default by print. Here are the places where Perl will assume $_ even if you don't specify it −
Various unary functions, including functions like ord and int, as well as the all file tests (-f, -d) except for -t, which defaults to STDIN.
Various unary functions, including functions like ord and int, as well as the all file tests (-f, -d) except for -t, which defaults to STDIN.
Various list functions like print and unlink.
Various list functions like print and unlink.
The pattern-matching operations m//, s///, and tr/// when used without an =~ operator.
The pattern-matching operations m//, s///, and tr/// when used without an =~ operator.
The default iterator variable in a foreach loop if no other variable is supplied.
The default iterator variable in a foreach loop if no other variable is supplied.
The implicit iterator variable in the grep and map functions.
The implicit iterator variable in the grep and map functions.
The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a while test (i.e., ). Note that outside of a while test, this will not happen.
The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a while test (i.e., ). Note that outside of a while test, this will not happen.
Based on the usage and nature of special variables, we can categorize them in the following categories −
Global Scalar Special Variables.
Global Array Special Variables.
Global Hash Special Variables.
Global Special Filehandles.
Global Special Constants.
Regular Expression Special Variables.
Filehandle Special Variables.
Here is the list of all the scalar special variables. We have listed corresponding english like names along with the symbolic names.

    46 Lectures 
     4.5 hours 

 Devi Killada

    11 Lectures 
     1.5 hours 

 Harshit Srivastava

    30 Lectures 
     6 hours 

 TELCOMA Global

    24 Lectures 
     2 hours 

 Mohammad Nauman

    68 Lectures 
     7 hours 

 Stone River ELearning

    58 Lectures 
     6.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page