Environment and %ENV

Reading %ENV

Perl exposes the process environment as the hash %ENV. It is a normal-looking hash, but every read goes to the real environment, so values are always current.

use strict; use warnings; print "user: $ENV{USER}\n"; print "home: $ENV{HOME}\n"; print "shell: $ENV{SHELL}\n"; print "no HOME\n" unless exists $ENV{HOME};

perl env.pl

user: ada home: /home/ada shell: /bin/bash

The whole list is one hash, so you can dump it when debugging a deployment: print "$_=$ENV{$_}\n" for sort keys %ENV. Do that on a server carefully, since the output can contain tokens.

Changing the environment

Assigning to %ENV changes the environment of the current process and of every program it starts later. Deleting a key removes the variable from children.

$ENV{APP_ENV} = "test"; $ENV{NO_COLOR} = 1; delete $ENV{DEBUG}; system("env");

The child inherits exactly this modified set. That is the usual way to pass configuration to a program you launch without adding flags to its command line.

To change one variable for a block of code and then restore it, use local. The old value comes back automatically when the block exits, even if the block dies:

{ local $ENV{APP_ENV} = "test"; system("prove", "-l", "t/"); } print "back to $ENV{APP_ENV}\n";

Defaults and paths

The defined-or operator turns a possibly missing variable into a usable value in one line, without a warning when the key is absent:

my $log = $ENV{LOG_FILE} // "/var/log/app.log"; my $port = $ENV{PORT} // 8080;

Note the difference from ||: defined-or keeps an empty string, while logical-or replaces it. For a port number you probably want to reject the empty string as well, so validate after reading instead of guessing.

PATH is a list glued together with a separator, a colon on Unix and a semicolon on Windows. Ask Config for the separator rather than hardcoding it:

use Config; my @dirs = split /\Q$Config{path_sep}\E/, $ENV{PATH}; print "$_ is on PATH\n" for @dirs;

The same idea applies to the home folder, which has a different name on Windows. Build a small helper once and use it everywhere:

sub home_dir { return $ENV{HOME} // $ENV{USERPROFILE} // die "cannot find the home folder\n"; }

Secrets and portability

Environment variables are convenient for configuration, but they are a poor place for passwords. On Linux any user who can see the process can read /proc/PID/environ, and the value is inherited by every child process, including ones that log their own environment.

Prefer a config file with tight permissions, or a secret manager that hands the value to the process that needs it. If you must use a variable, set it immediately before the one command that needs it and unset it afterwards.

local $ENV{PGPASSWORD} = $secret; system("psql", "-c", "select 1"); delete $ENV{PGPASSWORD};

Names are case-sensitive on Unix and case-insensitive on Windows, so read one exact spelling and do not rely on PATH versus Path. Under taint mode %ENV starts empty for security, which is another reason not to build a program that cannot run without it.

Finally, remember that environment variables are invisible to the person running your script next month. Print the effective settings at startup when --verbose is on, and document every variable you read in the help page.

Next: system, backticks and pipes

Article author: Arthur Isaev