7 Env - perl module that imports environment variables as scalars or arrays
12 use Env qw(PATH HOME TERM);
13 use Env qw($SHELL @LD_LIBRARY_PATH);
17 Perl maintains environment variables in a special hash named C<%ENV>. For
18 when this access method is inconvenient, the Perl module C<Env> allows
19 environment variables to be treated as scalar or array variables.
21 The C<Env::import()> function ties environment variables with suitable
22 names to global Perl variables with the same names. By default it
23 ties all existing environment variables (C<keys %ENV>) to scalars. If
24 the C<import> function receives arguments, it takes them to be a list of
25 variables to tie; it's okay if they don't yet exist. The scalar type
26 prefix '$' is inferred for any element of this list not prefixed by '$'
27 or '@'. Arrays are implemented in terms of C<split> and C<join>, using
28 C<$Config::Config{path_sep}> as the delimiter.
30 After an environment variable is tied, merely use it like a normal variable.
31 You may access its value
33 @path = split(/:/, $PATH);
34 print join("\n", @LD_LIBRARY_PATH), "\n";
39 push @LD_LIBRARY_PATH, $dir;
41 however you'd like. Bear in mind, however, that each access to a tied array
42 variable requires splitting the environment variable's string anew.
54 except that if C<$ENV{PATH}> started out empty, the second approach leaves
55 it with the (odd) value "C<:.>", but the first approach leaves it with "C<.>".
57 To remove a tied environment variable from
58 the environment, assign it the undefined value
61 undef @LD_LIBRARY_PATH;
65 On VMS systems, arrays tied to environment variables are read-only. Attempting
66 to change anything will cause a warning.
70 Chip Salzenberg E<lt>F<chip@fin.uucp>E<gt>
72 Gregor N. Purdy E<lt>F<gregor@focusresearch.com>E<gt>
77 my ($callpack) = caller(0);
79 my @vars = grep /^[\$\@]?[A-Za-z_]\w*$/, (@_ ? @_ : keys(%ENV));
82 @vars = map { m/^[\$\@]/ ? $_ : '$'.$_ } @vars;
84 eval "package $callpack; use vars qw(" . join(' ', @vars) . ")";
87 my ($type, $name) = m/^([\$\@])(.*)$/;
89 tie ${"${callpack}::$name"}, Env, $name;
92 tie @{"${callpack}::$name"}, Env::Array::VMS, $name;
94 tie @{"${callpack}::$name"}, Env::Array, $name;
110 my ($self, $value) = @_;
111 if (defined($value)) {
112 $ENV{$$self} = $value;
118 ######################################################################
125 @ISA = qw(Tie::Array);
127 my $sep = $Config::Config{path_sep};
135 return 1 + scalar(() = $ENV{$$self} =~ /\Q$sep\E/g);
139 my ($self, $size) = @_;
140 my @temp = split($sep, $ENV{$$self});
142 $ENV{$$self} = join($sep, @temp);
151 my ($self, $index) = @_;
152 return (split($sep, $ENV{$$self}))[$index];
156 my ($self, $index, $value) = @_;
157 my @temp = split($sep, $ENV{$$self});
158 $temp[$index] = $value;
159 $ENV{$$self} = join($sep, @temp);
164 my ($self, $index) = @_;
165 return $index < $self->FETCHSIZE;
169 my ($self, $index) = @_;
170 my @temp = split($sep, $ENV{$$self});
171 my $value = splice(@temp, $index, 1, ());
172 $ENV{$$self} = join($sep, @temp);
178 my @temp = split($sep, $ENV{$$self});
180 $ENV{$$self} = join($sep, @temp);
181 return scalar(@temp);
186 my @temp = split($sep, $ENV{$$self});
187 my $result = pop @temp;
188 $ENV{$$self} = join($sep, @temp);
194 my @temp = split($sep, $ENV{$$self});
195 my $result = unshift @temp, @_;
196 $ENV{$$self} = join($sep, @temp);
202 my @temp = split($sep, $ENV{$$self});
203 my $result = shift @temp;
204 $ENV{$$self} = join($sep, @temp);
212 my @temp = split($sep, $ENV{$$self});
214 my @result = splice @temp, $self, $offset, $length, @_;
215 $ENV{$$self} = join($sep, @temp);
218 my $result = scalar splice @temp, $offset, $length, @_;
219 $ENV{$$self} = join($sep, @temp);
224 ######################################################################
226 package Env::Array::VMS;
229 @ISA = qw(Tie::Array);
238 while ($i < 127 and defined $ENV{$$self . ';' . $i}) { $i++; };
243 my ($self, $index) = @_;
244 return $ENV{$$self . ';' . $index};
248 my ($self, $index) = @_;
249 return $index < $self->FETCHSIZE;