”;
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 list functions like print and unlink.
-
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 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.
Special Variable Types
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.
Global Scalar Special Variables
Here is the list of all the scalar special variables. We have listed corresponding english like names along with the symbolic names.
$_ | The default input and pattern-searching space. |
$ARG | |
$. | The current input line number of the last filehandle that was read. An explicit close on the filehandle resets the line number. |
$NR | |
$/ | The input record separator; newline by default. If set to the null string, it treats blank lines as delimiters. |
$RS | |
$, | The output field separator for the print operator. |
$OFS | |
$ | The output record separator for the print operator. |
$ORS | |
$” | Like “$,” except that it applies to list values interpolated into a double-quoted string (or similar interpreted string). Default is a space. |
$LIST_SEPARATOR | |
$; | The subscript separator for multidimensional array emulation. Default is “ |