home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl502b.zip / lib / Env.pm < prev    next >
Text File  |  1995-12-11  |  1KB  |  75 lines

  1. package Env;
  2.  
  3. =head1 NAME
  4.  
  5. Env - perl module that imports environment variables
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use Env;
  10.     use Env qw(PATH HOME TERM);
  11.  
  12. =head1 DESCRIPTION
  13.  
  14. Perl maintains environment variables in a pseudo-associative-array
  15. named %ENV.  For when this access method is inconvenient, the Perl
  16. module C<Env> allows environment variables to be treated as simple
  17. variables.
  18.  
  19. The Env::import() function ties environment variables with suitable
  20. names to global Perl variables with the same names.  By default it
  21. does so with all existing environment variables (C<keys %ENV>).  If
  22. the import function receives arguments, it takes them to be a list of
  23. environment variables to tie; it's okay if they don't yet exist.
  24.  
  25. After an environment variable is tied, merely use it like a normal variable.
  26. You may access its value 
  27.  
  28.     @path = split(/:/, $PATH);
  29.  
  30. or modify it
  31.  
  32.     $PATH .= ":.";
  33.  
  34. however you'd like.
  35. To remove a tied environment variable from
  36. the environment, assign it the undefined value
  37.  
  38.     undef $PATH;
  39.  
  40. =head1 AUTHOR
  41.  
  42. Chip Salzenberg <chip@fin.uucp>
  43.  
  44. =cut
  45.  
  46. sub 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.  
  56. sub TIESCALAR {
  57.     bless \($_[1]);
  58. }
  59.  
  60. sub FETCH {
  61.     my ($self) = @_;
  62.     $ENV{$$self};
  63. }
  64.  
  65. sub STORE {
  66.     my ($self, $value) = @_;
  67.     if (defined($value)) {
  68.     $ENV{$$self} = $value;
  69.     } else {
  70.     delete $ENV{$$self};
  71.     }
  72. }
  73.  
  74. 1;
  75.