patch for LWP 5.05 to make it play with both 5.003 and 5.003_20 + overload patch
[p5sagit/p5-mst-13.2.git] / lib / Env.pm
CommitLineData
a0d0e21e 1package Env;
2
3=head1 NAME
4
cb1a09d0 5Env - perl module that imports environment variables
6
7=head1 SYNOPSIS
8
9 use Env;
10 use Env qw(PATH HOME TERM);
a0d0e21e 11
12=head1 DESCRIPTION
13
14Perl maintains environment variables in a pseudo-associative-array
15named %ENV. For when this access method is inconvenient, the Perl
16module C<Env> allows environment variables to be treated as simple
17variables.
18
19The Env::import() function ties environment variables with suitable
20names to global Perl variables with the same names. By default it
21does so with all existing environment variables (C<keys %ENV>). If
22the import function receives arguments, it takes them to be a list of
23environment variables to tie; it's okay if they don't yet exist.
24
25After an environment variable is tied, merely use it like a normal variable.
26You may access its value
27
28 @path = split(/:/, $PATH);
29
30or modify it
31
32 $PATH .= ":.";
33
34however you'd like.
35To remove a tied environment variable from
36the environment, assign it the undefined value
37
38 undef $PATH;
39
40=head1 AUTHOR
41
1fef88e7 42Chip Salzenberg E<lt>F<chip@fin.uucp>E<gt>
a0d0e21e 43
44=cut
45
46sub import {
47 my ($callpack) = caller(0);
48 my $pack = shift;
49 my @vars = @_ ? @_ : keys(%ENV);
40da2db3 50 return unless @vars;
a0d0e21e 51
40da2db3 52 eval "package $callpack; use vars qw("
53 . join(' ', map { '$'.$_ } @vars) . ")";
54 die $@ if $@;
a0d0e21e 55 foreach (@vars) {
56 tie ${"${callpack}::$_"}, Env, $_ if /^[A-Za-z_]\w*$/;
57 }
58}
59
60sub TIESCALAR {
61 bless \($_[1]);
62}
63
64sub FETCH {
65 my ($self) = @_;
66 $ENV{$$self};
67}
68
69sub STORE {
70 my ($self, $value) = @_;
71 if (defined($value)) {
72 $ENV{$$self} = $value;
73 } else {
74 delete $ENV{$$self};
75 }
76}
77
781;