Special Variables: Default Variables and Parameters
Special Variables: Default Variables and Parameters
Special Variables: Default Variables and Parameters
'UnicornNovember' =~ /(U)\w?(N)/;
'UnicornNovember' =~ /(Uni)\w?(Nov)/;
532
Special Variables
Variable Description
@- This array lists the front pointer positions (in the referenced string) of the last
successful match. The first element @-[0] contains the pointer's starting position
prior to that match – each subsequent value corresponds to its position just before
having matched the corresponding grouped subpattern. For example following:
'UnicornNovember' =~ /(Uni)\w?(Nov)/;
'UnicornNovember' =~ /(Uni)(\w?)(Nov)/;
Input/Output Variables
Variable Description
$. This scalar holds the current line number of the last filehandle on which you
performed either a read, seek, or tell. It is reset when the filehandle is closed.
NB: <> never does an explicit close, so line numbers increase across ARGV files – also,
localizing $. has the effect of also localizing perl's notion of 'the last read filehandle'.
$/ This scalar stores the input record separator, which by default is the newline \n. If
it's set to "", input will be read one paragraph at a time.
$\ This scalar stores the output record separator for print – normally this will just
output consecutive records without any separation (unless explicitly included). This
variable allows you to set it for yourself. For example:
$\ = "-";
print "one";
print "two";
$, = "-";
print "one","two";
533
Appendix B
Variable Description
$" This is the output field separator for array values interpolated into a double-
quoted string (or similar interpreted string) – the default is a space. For example:
$" = "-";
@ar = ("one", "two", "three");
print "@ar";
will print one-two-three.
Filehandle/format Variables
Variable Description
$# This holds the output format for printed numbers.
NB: The use of this variable has been deprecated.
$| This corresponds to an internal flag used by perl to determine whether buffering
should be used on a program's write/read operations to/from files – if its value is
TRUE ($| is greater than 0), then buffering is disabled.
$% The current page number of the selected output channel.
$= The current page length, measured in printable lines – the default is 60.
This only becomes important when a top-of-page format is invoked – if a write
command doesn't fit into a given number of lines, then the top-of-page format is
used, before any printing past the page length continues.
$- The number of lines left on a page – when a page is finished, it's given the value of
$=, and is then decremented for each line outputted.
$~ The currently selected format name – the default is the name of the filehandle.
$^ The name of the top-of-page format.
$: The set of characters after which a string may be broken to fill continuation fields
(starting with ^) in a format – default is ' \n-' to break on whitespace or hyphens.
$^L This holds a character that is used by a format's output to request a form feed –
default is \f.
Error Variables
Variable Description
$? This holds the status value returned by the last pipe close, backtick (``) command,
or system() operator.
$@ This holds the syntax error message from the last eval() command – it evaluates
to null if the last eval() parsed and executed correctly (although the operations
you invoked may have failed in the normal fashion).
534
Special Variables
Variable Description
$! If used in a numeric context, this returns the current value of errno, with all the usual
caveats. (so you shouldn't depend on $! to have any particular value unless you've
got a specific error return indicating a system error.)
If used in a string context, it returns the corresponding system error string. You can
assign a set errno value to $! if, for instance, you want it to return the string for that
error number, or you want to set the exit value for the die() operator.
$^E This returns an extended error message, with information specific to the current
operating system. At the moment, this only differs from $! under VMS, OS/2, and
Win32 (and for MacPerl). On all other platforms, $^E is always the same as $!.
System Variables
Variable Description
$$ The process ID (pid) of the Perl process running the current script.
$< The real user ID (uid) of the current process.
$> The effective uid of the current process.
NB: $< and $> can only be swapped on machines supporting setreuid().
$( The real group ID (gid) of the current process.
$) The effective group ID (gid) of the current process.
$0 (zero) The name of the file containing the Perl script being executed.
$^X The name that the perl binary was executed as.
$] The version number of the perl interpreter, including patchlevel / 1000 – can be
used to determine whether the interpreter executing a script is within the right range
of versions.
See also use VERSION and require VERSION for a way to fail if the interpreter
is too old.
$^O The name of the operating system under which this copy of perl was built, as
determined during the configuration process – identical to $Config{'osname'}.
$^T The time at which the current script began running, in seconds since the beginning
of 1970. Values returned by -M, -A, and -C filetests are based on this value.
$^W The current value of the warning switch, either TRUE or FALSE.
%ENV Your current environment – altering its value changes the environment for child
processes.
%SIG Used to set handlers for various signals.
535
Appendix B
Others
Variable Description
@INC A list of places to look for Perl scripts for evaluation by the do EXPR, require,
or use constructs.
%INC Contains entries for each filename that has been included via do or require. The
key is the specified filename, and the value the location of the file actually found.
The require command uses this array to determine whether a given file has
already been included.
536
Special Variables
537
This work is licensed under the Creative Commons Attribution-NoDerivs-NonCommercial License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nd-nc/1.0 or send a letter to Creative Commons, 559 Nathan Abbott Way,
Stanford, California 94305, USA.
Attribution: The licensor permits others to copy, distribute, display, and perform the work. In return, licensees must give the
original author credit.
No Derivative Works: The licensor permits others to copy, distribute, display and perform only unaltered copies of the work --
not derivative works based on it.
Noncommercial: The licensor permits others to copy, distribute, display, and perform the work. In return, licensees may not
use the work for commercial purposes -- unless they get the licensor's permission.