home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / vars.pm < prev   
Text File  |  1997-05-18  |  2KB  |  54 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. Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
  18. C<use subs> declarations are not BLOCK-scoped.  They are thus effective
  19. for the entire file in which they appear.  You may not rescind such
  20. declarations with C<no vars> or C<no subs>.
  21.  
  22. Packages such as the B<AutoLoader> and B<SelfLoader> that delay
  23. loading of subroutines within packages can create problems with
  24. package lexicals defined using C<my()>. While the B<vars> pragma
  25. cannot duplicate the effect of package lexicals (total transparency
  26. outside of the package), it can act as an acceptable substitute by
  27. pre-declaring global symbols, ensuring their availability to the
  28. later-loaded routines.
  29.  
  30. See L<perlmod/Pragmatic Modules>.
  31.  
  32. =cut
  33.  
  34. require 5.002;
  35. use Carp;
  36.  
  37. sub import {
  38.     my $callpack = caller;
  39.     my ($pack, @imports, $sym, $ch) = @_;
  40.     foreach $sym (@imports) {
  41.     croak "Can't declare another package's variables" if $sym =~ /::/;
  42.         ($ch, $sym) = unpack('a1a*', $sym);
  43.         *{"${callpack}::$sym"} =
  44.           (  $ch eq "\$" ? \$   {"${callpack}::$sym"}
  45.            : $ch eq "\@" ? \@   {"${callpack}::$sym"}
  46.            : $ch eq "\%" ? \%   {"${callpack}::$sym"}
  47.            : $ch eq "\*" ? \*   {"${callpack}::$sym"}
  48.            : $ch eq "\&" ? \&   {"${callpack}::$sym"}
  49.            : croak "'$ch$sym' is not a valid variable name\n");
  50.     }
  51. };
  52.  
  53. 1;
  54.