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 env.pl:

use Env;

use strict;

my(@files);

opendir(DIR, $main::TEMP);

    @files = readdir(DIR);

closedir(DIR);

print "$main::TEMP\n";

foreach (@files) {

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

}

This program displays:

C:\WINDOWS\TEMP

        ~Df182.TMP

        ~Df1B3.TMP

        ~Df8073.TMP

        ~Df8074.TMP

        ~WRS0003.tmp

        ~Df6116.TMP

        ~DFC2C2.TMP

        ~Df9145.TMP

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.


dave@cs.cf.ac.uk