![]() |
perlvar
Perl 5 version 26.1 documentation
perlvarNAMEperlvar - Perl predefined variables DESCRIPTIONThe Syntax of Variable NamesVariable names in Perl can have several formats. Usually, they
must begin with a letter or underscore, in which case they can be
arbitrarily long (up to an internal limit of 251 characters) and
may contain letters, digits, underscores, or the special sequence
Perl variable names may also be a sequence of digits, a single
punctuation character, or the two-character sequence: Since Perl v5.6.0, Perl variable names may also be alphanumeric strings
preceded by a caret. These must all be written in the form Perl identifiers that begin with digits or
punctuation characters are exempt from the effects of the
In particular, the special SPECIAL VARIABLESThe following names have special meaning to Perl. Most punctuation names have reasonable mnemonics, or analogs in the shells. Nevertheless, if you wish to use long variable names, you need only say:
at the top of your program. This aliases all the short names to the long names in the current package. Some even have medium names, generally borrowed from awk. For more info, please see English. Before you continue, note the sort order for variables. In general, we
first list the variables in case-insensitive, almost-lexigraphical
order (ignoring the General Variables
Variables related to regular expressionsMost of the special variables related to regular expressions are side effects. Perl sets these variables when it has a successful match, so you should check the match result before using them. For instance: These variables are read-only and dynamically-scoped, unless we note otherwise. The dynamic nature of the regular expression variables means that their value is limited to the block that they are in, as demonstrated by this bit of code: The output shows that while in the
Performance issuesTraditionally in Perl, any use of any of the three variables In Perl 5.6.0 the In Perl 5.10.0 the In Perl 5.18.0 onwards, perl started noting the presence of each of the three variables separately, and only copied that part of the string required; so in
perl would only copy the "abcd" part of the string. That could make a big difference in something like
In Perl 5.20.0 a new copy-on-write system was enabled by default, which finally fixes all performance issues with these three variables, and makes them safe to use anywhere. The
Variables related to filehandlesVariables that depend on the currently selected filehandle may be set
by calling an appropriate object method on the
after which you may use either
or more safely,
Each method returns the old value of the Because loading in the A few of these variables are considered "read-only". This means that if you try to assign to this variable, either directly or indirectly through a reference, you'll raise a run-time exception. You should be very careful when modifying the default values of most special variables described in this document. In most cases you want to localize these variables before changing them, since if you don't, the change may affect other modules which rely on the default values of the special variables that you have changed. This is one of the correct ways to read the whole file at once: But the following code is quite bad: since some other module, may want to read data from some file in the
default "line mode", so if the code we have just presented has been
executed, the global value of Usually when a variable is localized you want to make sure that this
change affects the shortest scope possible. So unless you are already
inside some short Here is an example of how your own code can go broken: You probably expect this code to print the equivalent of
but instead you get:
Why? Because
It's easy to notice the problem in such a short example, but in more complicated code you are looking for trouble if you don't localize changes to the special variables.
Variables related to formatsThe special variables for formats are a subset of those for filehandles. See perlform for more information about Perl's formats.
Error VariablesThe variables To illustrate the differences between these variables, consider the following Perl expression, which uses a single-quoted string. After execution of this statement, perl may have set all four special error variables:
When perl executes the
Under a few operating systems, Finally, For more details, see the individual descriptions at
Variables related to the interpreter stateThese variables provide information about the current interpreter state.
Deprecated and removed variablesDeprecating a variable announces the intent of the perl maintainers to eventually remove the variable from the language. It may still be available despite its status. Using a deprecated variable triggers a warning. Once a variable is removed, its use triggers an error telling you the variable is unsupported. See perldiag for details about error messages.
|