Special Variables

    This is the same as

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

    $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.

    $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 contains all the paths that Perl will search to find a module.

    %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, 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 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.

    <> 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.


    Submit a PR to