This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to MakeMaker 5.34
[perl5.git] / lib / Env.pm
CommitLineData
a0d0e21e
LW
1package Env;
2
3=head1 NAME
4
cb1a09d0
AD
5Env - perl module that imports environment variables
6
7=head1 SYNOPSIS
8
9 use Env;
10 use Env qw(PATH HOME TERM);
a0d0e21e
LW
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
42Chip Salzenberg <chip@fin.uucp>
43
44=cut
45
46sub import {
47 my ($callpack) = caller(0);
48 my $pack = shift;
49 my @vars = @_ ? @_ : keys(%ENV);
50
51 foreach (@vars) {
52 tie ${"${callpack}::$_"}, Env, $_ if /^[A-Za-z_]\w*$/;
53 }
54}
55
56sub TIESCALAR {
57 bless \($_[1]);
58}
59
60sub FETCH {
61 my ($self) = @_;
62 $ENV{$$self};
63}
64
65sub STORE {
66 my ($self, $value) = @_;
67 if (defined($value)) {
68 $ENV{$$self} = $value;
69 } else {
70 delete $ENV{$$self};
71 }
72}
73
741;