1Env(3) User Contributed Perl Documentation Env(3)
2
3
4
6 Env - perl module that imports environment variables as scalars or
7 arrays
8
10 use Env;
11 use Env qw(PATH HOME TERM);
12 use Env qw($SHELL @LD_LIBRARY_PATH);
13
15 Perl maintains environment variables in a special hash named %ENV. For
16 when this access method is inconvenient, the Perl module "Env" allows
17 environment variables to be treated as scalar or array variables.
18
19 The Env::import() function ties environment variables with suitable
20 names to global Perl variables with the same names. By default it ties
21 all existing environment variables ("keys %ENV") to scalars. If the
22 "import" function receives arguments, it takes them to be a list of
23 variables to tie; it's okay if they don't yet exist. The scalar type
24 prefix '$' is inferred for any element of this list not prefixed by '$'
25 or '@'. Arrays are implemented in terms of "split" and "join", using
26 $Config::Config{path_sep} as the delimiter.
27
28 After an environment variable is tied, merely use it like a normal
29 variable. You may access its value
30
31 @path = split(/:/, $PATH);
32 print join("\n", @LD_LIBRARY_PATH), "\n";
33
34 or modify it
35
36 $PATH .= ":/any/path";
37 push @LD_LIBRARY_PATH, $dir;
38
39 however you'd like. Bear in mind, however, that each access to a tied
40 array variable requires splitting the environment variable's string
41 anew.
42
43 The code:
44
45 use Env qw(@PATH);
46 push @PATH, '/any/path';
47
48 is almost equivalent to:
49
50 use Env qw(PATH);
51 $PATH .= ":/any/path";
52
53 except that if $ENV{PATH} started out empty, the second approach leaves
54 it with the (odd) value "":/any/path"", but the first approach leaves
55 it with ""/any/path"".
56
57 To remove a tied environment variable from the environment, assign it
58 the undefined value
59
60 undef $PATH;
61 undef @LD_LIBRARY_PATH;
62
64 On VMS systems, arrays tied to environment variables are read-only.
65 Attempting to change anything will cause a warning.
66
68 Chip Salzenberg <chip@fin.uucp> and Gregor N. Purdy
69 <gregor@focusresearch.com>
70
71
72
73perl v5.36.0 2023-01-20 Env(3)