Special Variables

$_

$_ is the "it" variable.It's often the default parm that built-in functions use,or return into.

  1. while ( <> ) { # Read a line into $_
  2. print lc; # print lc($_)
  3. }

This is the same as

  1. while ( $it = <> ) {
  2. print lc($it);
  3. }

$0

$0 contains the name of the program being run, as given to the shell. If the program was run directly through the Perl interpreter, $0 contains the file name.

  1. $ cat file.pl
  2. #!/usr/bin/perl
  3. print $0, "\n";
  4. $ ./file.pl
  5. file.pl
  6. $ perl file.pl
  7. file.pl
  8. $ perl ./file.pl
  9. ./file.pl
  10. $ cat file.pl | perl
  11. -

$0 is what C programmers would expect to find as the first element of the argv array.

@ARGV

@ARGV contains the arguments given to the program, as ordered by the shell.

  1. $ perl -e 'print join( ", ", @ARGV), "\n"' 1 2 3
  2. 1, 2, 3
  3. $ perl -e 'print join( ", ", @ARGV), "\n"' 1 "2 3" 4
  4. 1, 2 3, 4

C programmers may be confused, since $ARGV[0] is really their argv[1]. Don't let this mistake happen to you!

@INC

@INC contains all the paths that Perl will search to find a module.

Perl programmers used to append or prepend to @INC to add a library path; these days, use lib is used instead. The following are mostly equivalent:

  1. BEGIN { unshift @INC, "local/lib" };
  2.  
  3. use lib "local/lib";

%ENV

%ENV contains a copy of the current environment. It is the environment that will be given to any subshell created by Perl.

This is significant in taint mode, as %ENV will have entries that can alter the shell's behavior. For that reason, perlsec recommends the following code be used prior to executing a command in taint mode:

  1. $ENV{'PATH'} = '/bin:/usr/bin'; # change to your real path
  2. delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

%SIG

Perl has rich signal handling capabilities; using the %SIG variable, you can make any subroutine run when a signal is sent to the running process.

This is especially useful if you have a long-running process, and would like to reload configuration files by sending a signal (usually SIGHUP) instead of having to start and stop the process.

You can also change the behavior of die and warn by assigning to $SIG{DIE} and $SIG{WARN}, respectively.

<>

The "diamond operator", <> is used when a program is expecting input, but isn't concerned how it arrives.

If the program receives any arguments, they are taken to be file names and their contents are sent through <>. Otherwise, standard input (STDIN) is used.

<> is especially useful for filter programs.

<DATA> and DATA

If a program contains the magic token DATA on a line by itself, anything following it will be available to the program through the magic <DATA> filehandle.

This is useful if you want to include data with your program, but want to keep it separated from the main program logic.

$!

When running any command that uses the system, $! will be true if the command returned a non-true status, or otherwise could not be run. $! will contain the error.

$@

If using eval, $@ contains the syntax error that the eval threw, if any.


Want to contribute?

Submit a PR to github.com/petdance/perl101