Env(3pm)

πŸ’– Env β€” the perl module that imports environment variables as scalars or arrays ✨

okay so bestie, this is Env(3pm) from the Perl Programmers Reference Guide, and it's genuinely one of those small-but-iconic modules. let's get into it 🫢


✨ SYNOPSIS ✨

use Env;
use Env qw(PATH HOME TERM);
use Env qw($SHELL @LD_LIBRARY_PATH);

like, that's it. that's the whole vibe check πŸ’…


🌸 DESCRIPTION 🌸

so. Perl keeps all your environment variables in a special hash called %ENV. iconic, love that for us. but sometimes that access method is just... inconvenient, y'know? and that's exactly when the Perl module Env swoops in like the main character β€” it lets environment variables be treated as scalar or array variables. no cap πŸ”₯

The Env::import() function ties environment variables with suitable names to global Perl variables with the same names. By default it ties all existing environment variables (keys %ENV) to scalars. so like, everything, at once. obsessed πŸ’œ

If the import function receives arguments, it takes them to be a list of variables to tie β€” and it's totally okay if they don't exist yet, no stress πŸ¦‹. The scalar type prefix '$' is inferred for any element of this list not prefixed by '$' or '@'. Arrays are implemented in terms of split and join, using $Config::Config{path_sep} as the delimiter. lowkey genius.

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

@path = split(/:/, $PATH);
print join("\n", @LD_LIBRARY_PATH), "\n";

or modify it:

$PATH .= ":/any/path";
push @LD_LIBRARY_PATH, $dir;

however you'd like, bestie, go off πŸ’…

🫢 but heads up: each access to a tied array variable requires splitting the environment variable's string anew. every single time. it's giving "no caching, we die like real ones" 😭

The code:

use Env qw(@PATH);
push @PATH, '/any/path';

is almost equivalent to:

use Env qw(PATH);
$PATH .= ":/any/path";

except that if $ENV{PATH} started out empty, the second approach leaves it with the (odd) value "":/any/path"", but the first approach leaves it with ""/any/path"". yeah. that's the difference. write it down πŸ“βœ¨

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

undef $PATH;
undef @LD_LIBRARY_PATH;

poof, gone 🧚


πŸ’€ LIMITATIONS πŸ’€

On VMS systems, arrays tied to environment variables are read-only. Attempting to change anything will cause a warning. so like, look but don't touch on VMS, fr fr πŸ™ˆ


πŸ‘‘ AUTHOR πŸ‘‘

Chip Salzenberg <chip@fin.uucp> and Gregor N. Purdy <gregor@focusresearch.com>

absolute legends, we stan 🌈⭐


and that's the page!! 🌸 this was Env(3pm) from perl v5.42.3, dated 2026-11-03 β€” mwah, bye bestie, go tie some variables πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from