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
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms