Env(3perl)
π NAME π
okay bestie so listen β Env is that perl module that imports environment variables as scalars or arrays. that's it, that's the whole vibe. she's simple, she's iconic, she does one thing and she does it well π
β¨ SYNOPSIS β¨
use Env;
use Env qw(PATH HOME TERM);
use Env qw($SHELL @LD_LIBRARY_PATH);
like. look at her. three lines and you already get it π«Ά
πΈ DESCRIPTION πΈ
so here's the tea: perl keeps your environment variables in a special hash called %ENV. very demure, very mindful. but sometimes accessing stuff through a hash is just... not the moment π and for those times, the Env module lets you treat environment variables like regular scalar or array variables instead. main character energy fr fr.
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. all of them. no cap, every single one β¨
If the import function receives arguments, it takes them to be a list of variables to tie β and it's totally fine if they don't exist yet, she's flexible like that π
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.
Once an environment variable is tied, you literally just use it like a normal variable. truly the way that she's just a variable now π¦
Go ahead and 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, it's your world π
small heads-up though: bear in mind that each access to a tied array variable requires splitting the environment variable's string anew. every. single. time. lowkey a lot of work for her but she does it without complaining π
Okay so this code:
use Env qw(@PATH);
push @PATH, '/any/path';
is almost equivalent to:
use Env qw(PATH);
$PATH .= ":/any/path";
almost. almost. because there's one lil difference π β 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". see the lil colon?? that's the whole difference. iconic. anyway.
And if you wanna remove a tied environment variable from the environment, just assign it the undefined value. bye bye bestie π
undef $PATH;
undef @LD_LIBRARY_PATH;
periodt. gone. not in the environment anymore π
β οΈ LIMITATIONS β οΈ
On VMS systems, arrays tied to environment variables are read-only. Trying to change literally anything will cause a warning. she said look but don't touch π« (not me finding out the hard way π)
π AUTHOR π
This masterpiece was brought to you by Chip Salzenberg <chip@fin.uucp> and Gregor N. Purdy <gregor@focusresearch.com>. legends, honestly. we stan π«Ά
aaaand that's the whole page bestie!! this was perl v5.44.0, dated 2026-10-17, living in the Env(3perl) section of your friendly neighborhood Perl Programmers Reference Guide. go forth and tie some variables, you've got this πβ¨πΈ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms