Easy Tutorial
❮ Perl Sending Email Perl If Statement ❯

Perl Special Variables

Perl defines some special variables, usually prefixed with $, @, or %, such as $_ .

Many special variables have a long English name; the operating system variable $! can be written as $OS_ERROR.

To use the English names of special variables, you need to add use English; at the top of your program. This allows you to use descriptive English special variables.

The most commonly used special variable is $_, which contains the default input and pattern matching content. Here is an example:

Example

#!/usr/bin/perl

foreach ('Google', 'tutorialpro', 'Taobao') {
    print $_;
    print "\n";
}

Executing the above program will output:

Google
tutorialpro
Taobao

In the following example, we do not use $_ to output content:

Example

#!/usr/bin/perl

foreach ('Google', 'tutorialpro', 'Taobao') {
    print;
    print "\n";
}

Executing the above program will output:

Google
tutorialpro
Taobao

In the example, "Google" is output first, followed by "tutorialpro", and finally "Taobao".

In the iteration loop, the current string is placed in $_, and then output via print. Additionally, print uses $_ by default when no output variable is specified.

Here are some places where Perl will assume the use of $_ even if it is not explicitly written:


Types of Special Variables

Special variables can be categorized based on their usage:

Global Scalar Special Variables

The following lists all scalar special variables, including those with special characters and English forms:

| $_ | Default input and pattern matching content. | | $ARG | | $. | Current line number of the last file handle read. | | $NR | | $/ | Input record separator, default is newline. If undef, reads to end of file. | | $RS | | $, | Output field separator. | | $OFS | | $\ | Output record separator. | | $ORS | | $" | Similar to $, but used for interpolating arrays and slices in double-quoted strings. Default is a space. | | $LIST_SEPARATOR | | $; | Separator used in emulating multidimensional arrays. Default is "\034". | | $SUBSCRIPT_SEPARATOR | | $^L | Form feed character sent to the output channel. Default is "\f". | | $FORMAT_FORMFEED | | $: | The current set of characters after which a string may be broken to fill continuation fields (starting with ^) in a format. Default is "\n". | | $FORMAT_LINE_BREAK_CHARACTERS | | $^A | Variable used to store formatted data before printing. | | $ACCUMULATOR | | $# | Default numeric output format for printing numbers (deprecated). | | $OFMT | | $? | Status of the last external command. | | $CHILD_ERROR | | $! | Numeric value is errno, string value is the corresponding system error string. | | $OS_ERROR or $ERRNO | | $@ | Error message from eval. If empty, the last eval succeeded. | | $EVAL_ERROR | | $$ | Process ID of the current Perl script. | | $PROCESS_ID or $PID | | $< | Real user ID of the current process. | | $REAL_USER_ID or $UID |

Special Variables in Perl

Variable Description
$EFFECTIVE_USER_ID or $EUID Effective user ID of the current process
$REAL_GROUP_ID or $GID Real group user ID of the current process
$EFFECTIVE_GROUP_ID or $EGID Effective group user ID of the current process
$0 Filename of the script being executed
$PROGRAM_NAME
$[ The index of the first element in an array, default is 0
$] Perl version number
$PERL_VERSION
$^D Value of the debugging flag
$DEBUGGING
$^E Extended error information in non-UNIX environments
$EXTENDED_OS_ERROR
$^F Maximum file descriptor number
$SYSTEM_FD_MAX
$^H Syntax check status activated by the compiler
$^I Value of the in-place editor builtin
$INPLACE_EDIT
$^M Size of the alternate memory pool
$^O Operating system name
$OSNAME
$^P Internal variable specifying the current debugging value
$PERLDB
$^T The time the script started running in seconds since the epoch
$BASETIME
$^W Current value of the warning switch
$WARNING
$^X Name of the Perl binary executable
$EXECUTABLE_NAME
$ARGV Current filename when reading from the default filehandle

Global Array Special Variables

Variable Description
@ARGV List of command-line arguments passed to the script
@INC List of directories to search for modules during import
@F Array input from the command line

Global Hash Special Variables

Variable Description
%INC Hash containing all files included with do or require. Key is the filename, value is the path
%ENV Contains current environment variables
%SIG List of signals and their handlers

Global Special File Handles

Handle Description
ARGV Special file handle to iterate over all filenames in the @ARGV array
STDERR Standard error output handle
STDIN Standard input handle
STDOUT Standard output handle
DATA Special file handle that references any content after the __END__ marker in the file, or all content after the __DATA__ marker, as long as you are reading within the same package
_ (underscore) Special file handle used to cache file information (fstat, stat, and lstat)

Global Special Constants

Constant Description
__END__ Logical end of the script, ignoring subsequent text
__FILE__ Current filename
__LINE__ Current line number
__PACKAGE__ Current package name, default is main

Regular Expression Special Variables

Variable Description
$n Contains the nth substring from the last pattern match
$& The string from the previous successful pattern match
$MATCH
$` The content before the substring that matched successfully in the previous match
$PREMATCH
$' The content after the substring that matched successfully in the previous match
$POSTMATCH
$+ The last bracket matched by the last search pattern. E.g., /Version: (.*) Revision: (.*)/ && ($rev = $+);
$LAST_PAREN_MATCH

File Handle Special Variables

Variable Description
$ If set to zero, automatically calls fflush after each write or print function to write content back to the file
$OUTPUT_AUTOFLUSH
$% Current output page number
$FORMAT_PAGE_NUMBER
$= Current page length. Default is 60.
$FORMAT_LINES_PER_PAGE
$- Number of lines remaining on the current page
$FORMAT_LINES_LEFT
$~ Name of the current report output format. Default is the file handle name.
$FORMAT_NAME
$^ Name of the current report header format. Default is the file handle name with "_TOP" suffix.
$FORMAT_TOP_NAME
❮ Perl Sending Email Perl If Statement ❯