next up previous contents
Next: Handling Errors and Signals Up: Special Variables Previous: Example: Using the DATA

Example: Using the %ENV Variable

Environment variables are used by the operating system to store bits of information that are needed to run the computer. They are called environment variables because you rarely need to use them and because they simply remain in the background-just another part of the overall computing environment of your system. When your Perl process is started, it is given a copy of the environment variables to use as needed.

You can change the environment variables, but the changes will not persist after the process running Perl is ended. The changes will, however, affect the current process and any child processes that are started.

You can print out the environment variables by using these lines of code (

foreach $key (keys(%ENV)) {

    printf("%-10.10s: $ENV{$key}\n", $key);


On a Windows 95 machine, this program displays the following:



PROMPT    : $p$g

CLASSPATH : .\;e:\jdk\classes;



CMDLINE   : perl -w

BLASTER   : A220 I10 D3 H7 P330 T6



TZ        : GMT-05:00

Only a few of these variables are interesting. The TMP and TEMP variables let you know where temporary files should be placed. The PATH variable lets the system know where to look for executable programs. It will search each directory in the list until the needed file is found. The TZ variable lets you know which time zone the computer is running in.

The most useful variable is probably the PATH statement. By changing it, you can force the system to search the directories you specify. This might be useful if you suspect that another program of the same name resides in another directory. By placing the current directory at the beginning of the PATH variable, it will be searched first and you'll always get the executable you want. For example:

$ENV{"PATH"} = ".;" . $ENV{"PATH"};

A single period is used to refer to the current directory, and a semicolon is used to delimit the directories in the PATH variable. So this statement forces the operating system to look in the current directory before searching the rest of the directories in PATH.

Environment variables can be useful if you want a quick way to pass information between a parent and a child process. The parent can set the variables, and the child can read it.

next up previous contents
Next: Handling Errors and Signals Up: Special Variables Previous: Example: Using the DATA