home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / lib / vars.pm < prev    next >
Text File  |  2000-03-12  |  3KB  |  80 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. use warnings::register();
  12.  
  13. sub import {
  14.     my $callpack = caller;
  15.     my ($pack, @imports, $sym, $ch) = @_;
  16.     foreach $sym (@imports) {
  17.         ($ch, $sym) = unpack('a1a*', $sym);
  18.     if ($sym =~ tr/A-Za-z_0-9//c) {
  19.         # time for a more-detailed check-up
  20.         if ($sym =~ /::/) {
  21.         require Carp;
  22.         Carp::croak("Can't declare another package's variables");
  23.         } elsif ($sym =~ /^\w+[[{].*[]}]$/) {
  24.         require Carp;
  25.         Carp::croak("Can't declare individual elements of hash or array");
  26.         } elsif (warnings::enabled() and length($sym) == 1 and $sym !~ tr/a-zA-Z//) {
  27.         warnings::warn("No need to declare built-in vars");
  28.         }
  29.     }
  30.         *{"${callpack}::$sym"} =
  31.           (  $ch eq "\$" ? \$   {"${callpack}::$sym"}
  32.            : $ch eq "\@" ? \@   {"${callpack}::$sym"}
  33.            : $ch eq "\%" ? \%   {"${callpack}::$sym"}
  34.            : $ch eq "\*" ? \*   {"${callpack}::$sym"}
  35.            : $ch eq "\&" ? \&   {"${callpack}::$sym"}
  36.            : do {
  37.         require Carp;
  38.         Carp::croak("'$ch$sym' is not a valid variable name");
  39.          });
  40.     }
  41. };
  42.  
  43. 1;
  44. __END__
  45.  
  46. =head1 NAME
  47.  
  48. vars - Perl pragma to predeclare global variable names (obsolete)
  49.  
  50. =head1 SYNOPSIS
  51.  
  52.     use vars qw($frob @mung %seen);
  53.  
  54. =head1 DESCRIPTION
  55.  
  56. NOTE: The functionality provided by this pragma has been superseded
  57. by C<our> declarations, available in Perl v5.6.0 or later.  See
  58. L<perlfunc/our>.
  59.  
  60. This will predeclare all the variables whose names are 
  61. in the list, allowing you to use them under "use strict", and
  62. disabling any typo warnings.
  63.  
  64. Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
  65. C<use subs> declarations are not BLOCK-scoped.  They are thus effective
  66. for the entire file in which they appear.  You may not rescind such
  67. declarations with C<no vars> or C<no subs>.
  68.  
  69. Packages such as the B<AutoLoader> and B<SelfLoader> that delay
  70. loading of subroutines within packages can create problems with
  71. package lexicals defined using C<my()>. While the B<vars> pragma
  72. cannot duplicate the effect of package lexicals (total transparency
  73. outside of the package), it can act as an acceptable substitute by
  74. pre-declaring global symbols, ensuring their availability to the
  75. later-loaded routines.
  76.  
  77. See L<perlmodlib/Pragmatic Modules>.
  78.  
  79. =cut
  80.