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.

75 lines
2.3 KiB

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