next up previous contents
Next: Debugging Perl Up: Module Examples Previous: The English Module

The Env Module

If you use environment variables a lot, then you need to look at the Env module. It will enable you to directly access the environment variables as Perl scalar variables instead of through the %Env hash. For example, $PATH is equivalent to $ENV{'PATH'}.

A simple program to illustrate is given below. It opeartes as follows:

The Perl code is

use Env;

use strict;


opendir(DIR, $main::TEMP);

    @files = readdir(DIR);


print "$main::TEMP\n";

foreach (@files) {

    print("\t$_\n") if m/\.tmp/i;


This program displays:










This program is pretty self-explanatory, except perhaps for the manner in which the $main::TEMP variable is specified. The strict pragma requires all variables to be lexically declared or to be fully qualified. The environment variables are declared in the Env package, but exported into the main namespace. Therefore, they need to be qualified using the main:: notation.