home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / mac / Software / entwickl / win95 / pw32i306.exe / lib / vars.pm < prev    next >
Text File  |  1996-08-16  |  1KB  |  47 lines

  1. package vars;
  2.  
  3. =head1 NAME
  4.  
  5. vars - Perl pragma to predeclare global variable names
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use vars qw($frob @mung %seen);
  10.  
  11. =head1 DESCRIPTION
  12.  
  13. This will predeclare all the variables whose names are 
  14. in the list, allowing you to use them under "use strict", and
  15. disabling any typo warnings.
  16.  
  17. Packages such as the B<AutoLoader> and B<SelfLoader> that delay loading
  18. of subroutines within packages can create problems with package lexicals
  19. defined using C<my()>. While the B<vars> pragma cannot duplicate the
  20. effect of package lexicals (total transparency outside of the package),
  21. it can act as an acceptable substitute by pre-declaring global symbols,
  22. ensuring their availability to to the later-loaded routines.
  23.  
  24. See L<perlmod/Pragmatic Modules>.
  25.  
  26. =cut
  27. require 5.000;
  28. use Carp;
  29.  
  30. sub import {
  31.     my $callpack = caller;
  32.     my ($pack, @imports, $sym, $ch) = @_;
  33.     foreach $sym (@imports) {
  34.     croak "Can't declare another package's variables" if $sym =~ /::/;
  35.         ($ch, $sym) = unpack('a1a*', $sym);
  36.         *{"${callpack}::$sym"} =
  37.           (  $ch eq "\$" ? \$   {"${callpack}::$sym"}
  38.            : $ch eq "\@" ? \@   {"${callpack}::$sym"}
  39.            : $ch eq "\%" ? \%   {"${callpack}::$sym"}
  40.            : $ch eq "\*" ? \*   {"${callpack}::$sym"}
  41.            : $ch eq "\&" ? \&   {"${callpack}::$sym"}
  42.            : croak "'$ch$sym' is not a valid variable name\n");
  43.     }
  44. };
  45.  
  46. 1;
  47.