What are special variables in Perl?

There are particularly reserved keywords for special variables in Perl. These keywords have predefined meanings.

Representation

These special variables are usually represented by using punctuation symbols right after a variable indicator. Variable indicators in Perl include , @, and %. An example of a declaration of a special variable would be $_.

Examples

Following are some examples of special variables in Perl:

$_

This variable searches for a pattern.

$/

This variable puts a newline in the input. It is called the 'input record separator'

$\

This variable puts a newline in the output printed. It is called the 'output record separator'

$"

This variable separates the values in a list.

$0

This value stores the name of the file containing the running Perl code.

%SIG

This variable represents the hash used to set signal handlers for many signals

@ARGV

This variable stores an array containing CLI arguments required in the code.



Code

The following code outlines the use of special variables in Perl:

# this code example shows the special variables $_ and $/
foreach ('This','is','an', 'Edpresso', 'shot.') {
# the '$_' special variable can be used to iterate over a list (pattern)
print $_;
# the '$/' special variable can be used to put a new line
print $/;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved