Main Content

Environment variables in Perl / use Env

Archive - Originally posted on "The Horse's Mouth" - 2008-07-11 06:35:49 - Graham Ellis

If I say use Env;, Perl loads in a module which allows me to access all my environment variables directly within my code, by name. If I add an explicit list of variable names as a list to the module, then only those variables will be imported. So in this example, $PATH prints nothing but $SHELL and $PWD tell you the shell and the present working directory. Extra environment variables can be accessed directly from the %ENV hash (and that can be done whether or not you have called the module in).

# Env brings environment into current namespace
use Env qw(SHELL PWD);
 
print "--- $PATH ---\n";
print "--- $SHELL ---\n";
print "--- $PWD ---\n";
 
print "--- $ENV{PATH} ---\n";