package Env;

#
# This package provides a handy interface for using environment
# variables as if they were normal Perl variables.
#
# By default it imports all environment variables that have legal
# Perl variable names.  If you give specific variable names, they
# can be imported too, even if they don't (yet) exist.  To delete
# a variable from the environment, assign "undef" to it.
#
# Author: Chip Salzenberg <chip@fin.uucp>
#

sub import {
    my ($callpack) = caller(0);
    my $pack = shift;
    my @vars = @_ ? @_ : keys(%ENV);

    foreach (@vars) {
	tie ${"${callpack}::$_"}, Env, $_ if /^[A-Za-z_]\w*$/;
    }
}

sub TIESCALAR {
    bless \($_[1]);
}

sub FETCH {
    my ($self) = @_;
    $ENV{$$self};
}

sub STORE {
    my ($self, $value) = @_;
    if (defined($value)) {
	$ENV{$$self} = $value;
    } else {
	delete $ENV{$$self};
    }
}

1;
