5 Env - perl module that imports environment variables as scalars or arrays
10 use Env qw(PATH HOME TERM);
11 use Env qw($SHELL @LD_LIBRARY_PATH);
15 Perl maintains environment variables in a special hash named C<%ENV>. For
16 when this access method is inconvenient, the Perl module C<Env> allows
17 environment variables to be treated as scalar or array variables.
19 The C<Env::import()> function ties environment variables with suitable
20 names to global Perl variables with the same names. By default it
21 ties all existing environment variables (C<keys %ENV>) to scalars. If
22 the C<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 C<split> and C<join>, using
26 C<$Config::Config{path_sep}> as the delimiter.
28 After an environment variable is tied, merely use it like a normal variable.
29 You may access its value
31 @path = split(/:/, $PATH);
32 print join("\n", @LD_LIBRARY_PATH), "\n";
37 push @LD_LIBRARY_PATH, $dir;
39 however you'd like. Bear in mind, however, that each access to a tied array
40 variable requires splitting the environment variable's string anew.
52 except that if C<$ENV{PATH}> started out empty, the second approach leaves
53 it with the (odd) value "C<:.>", but the first approach leaves it with "C<.>".
55 To remove a tied environment variable from
56 the environment, assign it the undefined value
59 undef @LD_LIBRARY_PATH;
63 On VMS systems, arrays tied to environment variables are read-only. Attempting
64 to change anything will cause a warning.
68 Chip Salzenberg E<lt>F<chip@fin.uucp>E<gt>
70 Gregor N. Purdy E<lt>F<gregor@focusresearch.com>E<gt>
75 my ($callpack) = caller(0);
77 my @vars = grep /^[\$\@]?[A-Za-z_]\w*$/, (@_ ?
@_ : keys(%ENV));
80 @vars = map { m/^[\$\@]/ ?
$_ : '$'.$_ } @vars;
82 eval "package $callpack; use vars qw(" . join(' ', @vars) . ")";
85 my ($type, $name) = m/^([\$\@])(.*)$/;
87 tie
${"${callpack}::$name"}, Env
, $name;
90 tie @
{"${callpack}::$name"}, Env
::Array
::VMS
, $name;
92 tie @
{"${callpack}::$name"}, Env
::Array
, $name;
108 my ($self, $value) = @_;
109 if (defined($value)) {
110 $ENV{$$self} = $value;
116 ######################################################################
123 @ISA = qw(Tie::Array);
125 my $sep = $Config::Config
{path_sep
};
133 my @temp = split($sep, $ENV{$$self});
134 return scalar(@temp);
138 my ($self, $size) = @_;
139 my @temp = split($sep, $ENV{$$self});
141 $ENV{$$self} = join($sep, @temp);
150 my ($self, $index) = @_;
151 return (split($sep, $ENV{$$self}))[$index];
155 my ($self, $index, $value) = @_;
156 my @temp = split($sep, $ENV{$$self});
157 $temp[$index] = $value;
158 $ENV{$$self} = join($sep, @temp);
164 my @temp = split($sep, $ENV{$$self});
166 $ENV{$$self} = join($sep, @temp);
167 return scalar(@temp);
172 my @temp = split($sep, $ENV{$$self});
173 my $result = pop @temp;
174 $ENV{$$self} = join($sep, @temp);
180 my @temp = split($sep, $ENV{$$self});
181 my $result = unshift @temp, @_;
182 $ENV{$$self} = join($sep, @temp);
188 my @temp = split($sep, $ENV{$$self});
189 my $result = shift @temp;
190 $ENV{$$self} = join($sep, @temp);
198 my @temp = split($sep, $ENV{$$self});
200 my @result = splice @temp, $self, $offset, $length, @_;
201 $ENV{$$self} = join($sep, @temp);
204 my $result = scalar splice @temp, $offset, $length, @_;
205 $ENV{$$self} = join($sep, @temp);
210 ######################################################################
212 package Env
::Array
::VMS
;
215 @ISA = qw(Tie::Array);
224 while ($i < 127 and defined $ENV{$$self . ';' . $i}) { $i++; };
229 my ($self, $index) = @_;
230 return $ENV{$$self . ';' . $index};