Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

  1. package Env;
  2. =head1 NAME
  3. Env - perl module that imports environment variables
  4. =head1 SYNOPSIS
  5. use Env;
  6. use Env qw(PATH HOME TERM);
  7. =head1 DESCRIPTION
  8. Perl maintains environment variables in a pseudo-hash named %ENV. For
  9. when this access method is inconvenient, the Perl module C<Env> allows
  10. environment variables to be treated as simple variables.
  11. The Env::import() function ties environment variables with suitable
  12. names to global Perl variables with the same names. By default it
  13. does so with all existing environment variables (C<keys %ENV>). If
  14. the import function receives arguments, it takes them to be a list of
  15. environment variables to tie; it's okay if they don't yet exist.
  16. After an environment variable is tied, merely use it like a normal variable.
  17. You may access its value
  18. @path = split(/:/, $PATH);
  19. or modify it
  20. $PATH .= ":.";
  21. however you'd like.
  22. To remove a tied environment variable from
  23. the environment, assign it the undefined value
  24. undef $PATH;
  25. =head1 AUTHOR
  26. Chip Salzenberg E<lt>F<[email protected]>E<gt>
  27. =cut
  28. sub import {
  29. my ($callpack) = caller(0);
  30. my $pack = shift;
  31. my @vars = grep /^[A-Za-z_]\w*$/, (@_ ? @_ : keys(%ENV));
  32. return unless @vars;
  33. eval "package $callpack; use vars qw("
  34. . join(' ', map { '$'.$_ } @vars) . ")";
  35. die $@ if $@;
  36. foreach (@vars) {
  37. tie ${"${callpack}::$_"}, Env, $_;
  38. }
  39. }
  40. sub TIESCALAR {
  41. bless \($_[1]);
  42. }
  43. sub FETCH {
  44. my ($self) = @_;
  45. $ENV{$$self};
  46. }
  47. sub STORE {
  48. my ($self, $value) = @_;
  49. if (defined($value)) {
  50. $ENV{$$self} = $value;
  51. } else {
  52. delete $ENV{$$self};
  53. }
  54. }
  55. 1;