home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / vars.pm < prev   
Text File  |  1997-11-25  |  2KB  |  67 lines

  1. package vars;
  2.  
  3. require 5.002;
  4.  
  5. # The following require can't be removed during maintenance
  6. # releases, sadly, because of the risk of buggy code that does
  7. # require Carp; Carp::croak "..."; without brackets dying
  8. # if Carp hasn't been loaded in earlier compile time. :-(
  9. # We'll let those bugs get found on the development track.
  10. require Carp if $] < 5.00450;
  11.  
  12. sub import {
  13.     my $callpack = caller;
  14.     my ($pack, @imports, $sym, $ch) = @_;
  15.     foreach $sym (@imports) {
  16.     if ($sym =~ /::/) {
  17.         require Carp;
  18.         Carp::croak("Can't declare another package's variables");
  19.     }
  20.         ($ch, $sym) = unpack('a1a*', $sym);
  21.         *{"${callpack}::$sym"} =
  22.           (  $ch eq "\$" ? \$   {"${callpack}::$sym"}
  23.            : $ch eq "\@" ? \@   {"${callpack}::$sym"}
  24.            : $ch eq "\%" ? \%   {"${callpack}::$sym"}
  25.            : $ch eq "\*" ? \*   {"${callpack}::$sym"}
  26.            : $ch eq "\&" ? \&   {"${callpack}::$sym"}
  27.            : do {
  28.         require Carp;
  29.         Carp::croak("'$ch$sym' is not a valid variable name\n");
  30.          });
  31.     }
  32. };
  33.  
  34. 1;
  35. __END__
  36.  
  37. =head1 NAME
  38.  
  39. vars - Perl pragma to predeclare global variable names
  40.  
  41. =head1 SYNOPSIS
  42.  
  43.     use vars qw($frob @mung %seen);
  44.  
  45. =head1 DESCRIPTION
  46.  
  47. This will predeclare all the variables whose names are 
  48. in the list, allowing you to use them under "use strict", and
  49. disabling any typo warnings.
  50.  
  51. Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
  52. C<use subs> declarations are not BLOCK-scoped.  They are thus effective
  53. for the entire file in which they appear.  You may not rescind such
  54. declarations with C<no vars> or C<no subs>.
  55.  
  56. Packages such as the B<AutoLoader> and B<SelfLoader> that delay
  57. loading of subroutines within packages can create problems with
  58. package lexicals defined using C<my()>. While the B<vars> pragma
  59. cannot duplicate the effect of package lexicals (total transparency
  60. outside of the package), it can act as an acceptable substitute by
  61. pre-declaring global symbols, ensuring their availability to the
  62. later-loaded routines.
  63.  
  64. See L<perlmod/Pragmatic Modules>.
  65.  
  66. =cut
  67.