xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Env.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Env;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateour $VERSION = '1.00';
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 NAME
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateEnv - perl module that imports environment variables as scalars or arrays
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate=head1 SYNOPSIS
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate    use Env;
12*0Sstevel@tonic-gate    use Env qw(PATH HOME TERM);
13*0Sstevel@tonic-gate    use Env qw($SHELL @LD_LIBRARY_PATH);
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate=head1 DESCRIPTION
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gatePerl maintains environment variables in a special hash named C<%ENV>.  For
18*0Sstevel@tonic-gatewhen this access method is inconvenient, the Perl module C<Env> allows
19*0Sstevel@tonic-gateenvironment variables to be treated as scalar or array variables.
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gateThe C<Env::import()> function ties environment variables with suitable
22*0Sstevel@tonic-gatenames to global Perl variables with the same names.  By default it
23*0Sstevel@tonic-gateties all existing environment variables (C<keys %ENV>) to scalars.  If
24*0Sstevel@tonic-gatethe C<import> function receives arguments, it takes them to be a list of
25*0Sstevel@tonic-gatevariables to tie; it's okay if they don't yet exist. The scalar type
26*0Sstevel@tonic-gateprefix '$' is inferred for any element of this list not prefixed by '$'
27*0Sstevel@tonic-gateor '@'. Arrays are implemented in terms of C<split> and C<join>, using
28*0Sstevel@tonic-gateC<$Config::Config{path_sep}> as the delimiter.
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gateAfter an environment variable is tied, merely use it like a normal variable.
31*0Sstevel@tonic-gateYou may access its value
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate    @path = split(/:/, $PATH);
34*0Sstevel@tonic-gate    print join("\n", @LD_LIBRARY_PATH), "\n";
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gateor modify it
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate    $PATH .= ":.";
39*0Sstevel@tonic-gate    push @LD_LIBRARY_PATH, $dir;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gatehowever you'd like. Bear in mind, however, that each access to a tied array
42*0Sstevel@tonic-gatevariable requires splitting the environment variable's string anew.
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gateThe code:
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate    use Env qw(@PATH);
47*0Sstevel@tonic-gate    push @PATH, '.';
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateis equivalent to:
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate    use Env qw(PATH);
52*0Sstevel@tonic-gate    $PATH .= ":.";
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gateexcept that if C<$ENV{PATH}> started out empty, the second approach leaves
55*0Sstevel@tonic-gateit with the (odd) value "C<:.>", but the first approach leaves it with "C<.>".
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gateTo remove a tied environment variable from
58*0Sstevel@tonic-gatethe environment, assign it the undefined value
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate    undef $PATH;
61*0Sstevel@tonic-gate    undef @LD_LIBRARY_PATH;
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate=head1 LIMITATIONS
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gateOn VMS systems, arrays tied to environment variables are read-only. Attempting
66*0Sstevel@tonic-gateto change anything will cause a warning.
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate=head1 AUTHOR
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gateChip Salzenberg E<lt>F<chip@fin.uucp>E<gt>
71*0Sstevel@tonic-gateand
72*0Sstevel@tonic-gateGregor N. Purdy E<lt>F<gregor@focusresearch.com>E<gt>
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate=cut
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gatesub import {
77*0Sstevel@tonic-gate    my ($callpack) = caller(0);
78*0Sstevel@tonic-gate    my $pack = shift;
79*0Sstevel@tonic-gate    my @vars = grep /^[\$\@]?[A-Za-z_]\w*$/, (@_ ? @_ : keys(%ENV));
80*0Sstevel@tonic-gate    return unless @vars;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate    @vars = map { m/^[\$\@]/ ? $_ : '$'.$_ } @vars;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate    eval "package $callpack; use vars qw(" . join(' ', @vars) . ")";
85*0Sstevel@tonic-gate    die $@ if $@;
86*0Sstevel@tonic-gate    foreach (@vars) {
87*0Sstevel@tonic-gate	my ($type, $name) = m/^([\$\@])(.*)$/;
88*0Sstevel@tonic-gate	if ($type eq '$') {
89*0Sstevel@tonic-gate	    tie ${"${callpack}::$name"}, Env, $name;
90*0Sstevel@tonic-gate	} else {
91*0Sstevel@tonic-gate	    if ($^O eq 'VMS') {
92*0Sstevel@tonic-gate		tie @{"${callpack}::$name"}, Env::Array::VMS, $name;
93*0Sstevel@tonic-gate	    } else {
94*0Sstevel@tonic-gate		tie @{"${callpack}::$name"}, Env::Array, $name;
95*0Sstevel@tonic-gate	    }
96*0Sstevel@tonic-gate	}
97*0Sstevel@tonic-gate    }
98*0Sstevel@tonic-gate}
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gatesub TIESCALAR {
101*0Sstevel@tonic-gate    bless \($_[1]);
102*0Sstevel@tonic-gate}
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gatesub FETCH {
105*0Sstevel@tonic-gate    my ($self) = @_;
106*0Sstevel@tonic-gate    $ENV{$$self};
107*0Sstevel@tonic-gate}
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gatesub STORE {
110*0Sstevel@tonic-gate    my ($self, $value) = @_;
111*0Sstevel@tonic-gate    if (defined($value)) {
112*0Sstevel@tonic-gate	$ENV{$$self} = $value;
113*0Sstevel@tonic-gate    } else {
114*0Sstevel@tonic-gate	delete $ENV{$$self};
115*0Sstevel@tonic-gate    }
116*0Sstevel@tonic-gate}
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate######################################################################
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gatepackage Env::Array;
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gateuse Config;
123*0Sstevel@tonic-gateuse Tie::Array;
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate@ISA = qw(Tie::Array);
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gatemy $sep = $Config::Config{path_sep};
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gatesub TIEARRAY {
130*0Sstevel@tonic-gate    bless \($_[1]);
131*0Sstevel@tonic-gate}
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gatesub FETCHSIZE {
134*0Sstevel@tonic-gate    my ($self) = @_;
135*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
136*0Sstevel@tonic-gate    return scalar(@temp);
137*0Sstevel@tonic-gate}
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gatesub STORESIZE {
140*0Sstevel@tonic-gate    my ($self, $size) = @_;
141*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
142*0Sstevel@tonic-gate    $#temp = $size - 1;
143*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
144*0Sstevel@tonic-gate}
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gatesub CLEAR {
147*0Sstevel@tonic-gate    my ($self) = @_;
148*0Sstevel@tonic-gate    $ENV{$$self} = '';
149*0Sstevel@tonic-gate}
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gatesub FETCH {
152*0Sstevel@tonic-gate    my ($self, $index) = @_;
153*0Sstevel@tonic-gate    return (split($sep, $ENV{$$self}))[$index];
154*0Sstevel@tonic-gate}
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gatesub STORE {
157*0Sstevel@tonic-gate    my ($self, $index, $value) = @_;
158*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
159*0Sstevel@tonic-gate    $temp[$index] = $value;
160*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
161*0Sstevel@tonic-gate    return $value;
162*0Sstevel@tonic-gate}
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gatesub PUSH {
165*0Sstevel@tonic-gate    my $self = shift;
166*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
167*0Sstevel@tonic-gate    push @temp, @_;
168*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
169*0Sstevel@tonic-gate    return scalar(@temp);
170*0Sstevel@tonic-gate}
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gatesub POP {
173*0Sstevel@tonic-gate    my ($self) = @_;
174*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
175*0Sstevel@tonic-gate    my $result = pop @temp;
176*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
177*0Sstevel@tonic-gate    return $result;
178*0Sstevel@tonic-gate}
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gatesub UNSHIFT {
181*0Sstevel@tonic-gate    my $self = shift;
182*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
183*0Sstevel@tonic-gate    my $result = unshift @temp, @_;
184*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
185*0Sstevel@tonic-gate    return $result;
186*0Sstevel@tonic-gate}
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gatesub SHIFT {
189*0Sstevel@tonic-gate    my ($self) = @_;
190*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
191*0Sstevel@tonic-gate    my $result = shift @temp;
192*0Sstevel@tonic-gate    $ENV{$$self} = join($sep, @temp);
193*0Sstevel@tonic-gate    return $result;
194*0Sstevel@tonic-gate}
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gatesub SPLICE {
197*0Sstevel@tonic-gate    my $self = shift;
198*0Sstevel@tonic-gate    my $offset = shift;
199*0Sstevel@tonic-gate    my $length = shift;
200*0Sstevel@tonic-gate    my @temp = split($sep, $ENV{$$self});
201*0Sstevel@tonic-gate    if (wantarray) {
202*0Sstevel@tonic-gate	my @result = splice @temp, $self, $offset, $length, @_;
203*0Sstevel@tonic-gate	$ENV{$$self} = join($sep, @temp);
204*0Sstevel@tonic-gate	return @result;
205*0Sstevel@tonic-gate    } else {
206*0Sstevel@tonic-gate	my $result = scalar splice @temp, $offset, $length, @_;
207*0Sstevel@tonic-gate	$ENV{$$self} = join($sep, @temp);
208*0Sstevel@tonic-gate	return $result;
209*0Sstevel@tonic-gate    }
210*0Sstevel@tonic-gate}
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate######################################################################
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gatepackage Env::Array::VMS;
215*0Sstevel@tonic-gateuse Tie::Array;
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate@ISA = qw(Tie::Array);
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gatesub TIEARRAY {
220*0Sstevel@tonic-gate    bless \($_[1]);
221*0Sstevel@tonic-gate}
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gatesub FETCHSIZE {
224*0Sstevel@tonic-gate    my ($self) = @_;
225*0Sstevel@tonic-gate    my $i = 0;
226*0Sstevel@tonic-gate    while ($i < 127 and defined $ENV{$$self . ';' . $i}) { $i++; };
227*0Sstevel@tonic-gate    return $i;
228*0Sstevel@tonic-gate}
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gatesub FETCH {
231*0Sstevel@tonic-gate    my ($self, $index) = @_;
232*0Sstevel@tonic-gate    return $ENV{$$self . ';' . $index};
233*0Sstevel@tonic-gate}
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate1;
236