Programming Perl

Programming PerlSearch this book
Previous: 7.2.13 English - Use English or awk Names for Punctuation VariablesChapter 7
The Standard Perl Library
Next: 7.2.15 Exporter - Default Import Method for Modules
 

7.2.14 Env - Import Environment Variables

use Env;                     # import all possible variables
use Env qw(PATH HOME TERM);  # import only specified variables

Perl maintains environment variables in a pseudo-associative array named %ENV. Since this access method is sometimes inconvenient, the Env module allows environment variables to be treated as simple variables.

The Env::import() routine ties environment variables to global Perl variables with the same names. By default it ties suitable, existing environment variables (that is, variables yielded by keys %ENV). An environmental variable is considered suitable if its name begins with an alphabetic character, and if it consists of nothing but alphanumeric characters plus underscore.

If you supply arguments when invoking use Env, they are taken to be a list of environment variables to tie. It's OK if the variables don't yet exist.

After an environment variable is tied, you can use it like a normal variable. You may access its value:

@path = split(/:/, $PATH);

or modify it any way you like:

$PATH .= ":.";

To remove a tied environment variable from the environment, make it the undefined value:

undef $PATH;

Note that the corresponding operation performed directly against %ENV is not undef, but delete:

delete $ENV{PATH};


Previous: 7.2.13 English - Use English or awk Names for Punctuation VariablesProgramming PerlNext: 7.2.15 Exporter - Default Import Method for Modules
7.2.13 English - Use English or awk Names for Punctuation VariablesBook Index7.2.15 Exporter - Default Import Method for Modules