Env - perl module that imports environment variables as scalars or arrays
use Env qw(PATH HOME TERM);
use Env qw($SHELL @LD_LIBRARY_PATH);
Perl maintains environment variables in a special hash named C<%ENV>. For
when this access method is inconvenient, the Perl module C<Env> allows
environment variables to be treated as scalar or array variables.
The C<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 (C<keys %ENV>) to scalars. If
the C<import> function receives arguments, it takes them to be a list of
variables to tie; it's okay if they don't yet exist. The scalar type
prefix '$' is inferred for any element of this list not prefixed by '$'
or '@'. Arrays are implemented in terms of C<split> and C<join>, using
C<$Config::Config{path_sep}> as the delimiter.
After an environment variable is tied, merely use it like a normal variable.
@path = split(/:/, $PATH);
print join("\n", @LD_LIBRARY_PATH), "\n";
push @LD_LIBRARY_PATH, $dir;
however you'd like. Bear in mind, however, that each access to a tied array
variable requires splitting the environment variable's string anew.
except that if C<$ENV{PATH}> started out empty, the second approach leaves
it with the (odd) value "C<:.>", but the first approach leaves it with "C<.>".
To remove a tied environment variable from
the environment, assign it the undefined value
On VMS systems, arrays tied to environment variables are read-only. Attempting
to change anything will cause a warning.
Chip Salzenberg E<lt>F<chip@fin.uucp>E<gt>
Gregor N. Purdy E<lt>F<gregor@focusresearch.com>E<gt>
my ($callpack) = caller(0);
my @vars = grep /^[\$\@]?[A-Za-z_]\w*$/, (@_ ?
@_ : keys(%ENV));
@vars = map { m/^[\$\@]/ ?
$_ : '$'.$_ } @vars;
eval "package $callpack; use vars qw(" . join(' ', @vars) . ")";
my ($type, $name) = m/^([\$\@])(.*)$/;
tie
${"${callpack}::$name"}, Env
, $name;
tie @
{"${callpack}::$name"}, Env
::Array
::VMS
, $name;
tie @
{"${callpack}::$name"}, Env
::Array
, $name;
######################################################################
my $sep = $Config::Config
{path_sep
};
my @temp = split($sep, $ENV{$$self});
my @temp = split($sep, $ENV{$$self});
$ENV{$$self} = join($sep, @temp);
return (split($sep, $ENV{$$self}))[$index];
my ($self, $index, $value) = @_;
my @temp = split($sep, $ENV{$$self});
$ENV{$$self} = join($sep, @temp);
my @temp = split($sep, $ENV{$$self});
$ENV{$$self} = join($sep, @temp);
my @temp = split($sep, $ENV{$$self});
$ENV{$$self} = join($sep, @temp);
my @temp = split($sep, $ENV{$$self});
my $result = unshift @temp, @_;
$ENV{$$self} = join($sep, @temp);
my @temp = split($sep, $ENV{$$self});
my $result = shift @temp;
$ENV{$$self} = join($sep, @temp);
my @temp = split($sep, $ENV{$$self});
my @result = splice @temp, $self, $offset, $length, @_;
$ENV{$$self} = join($sep, @temp);
my $result = scalar splice @temp, $offset, $length, @_;
$ENV{$$self} = join($sep, @temp);
######################################################################
while ($i < 127 and defined $ENV{$$self . ';' . $i}) { $i++; };
return $ENV{$$self . ';' . $index};