Commit | Line | Data |
c07a80fd |
1 | package vars; |
2 | |
fb73857a |
3 | require 5.002; |
4 | |
b75c8c73 |
5 | our $VERSION = '1.00'; |
6 | |
fb73857a |
7 | # The following require can't be removed during maintenance |
8 | # releases, sadly, because of the risk of buggy code that does |
9 | # require Carp; Carp::croak "..."; without brackets dying |
10 | # if Carp hasn't been loaded in earlier compile time. :-( |
11 | # We'll let those bugs get found on the development track. |
12 | require Carp if $] < 5.00450; |
3d842d7f |
13 | |
14 | use warnings::register; |
1d76c68a |
15 | require strict; |
fb73857a |
16 | |
17 | sub import { |
18 | my $callpack = caller; |
19 | my ($pack, @imports, $sym, $ch) = @_; |
20 | foreach $sym (@imports) { |
fb73857a |
21 | ($ch, $sym) = unpack('a1a*', $sym); |
014aafb8 |
22 | if ($sym =~ tr/A-Za-z_0-9//c) { |
15313f9c |
23 | # time for a more-detailed check-up |
24 | if ($sym =~ /::/) { |
25 | require Carp; |
26 | Carp::croak("Can't declare another package's variables"); |
27 | } elsif ($sym =~ /^\w+[[{].*[]}]$/) { |
28 | require Carp; |
29 | Carp::croak("Can't declare individual elements of hash or array"); |
d3a7d8c7 |
30 | } elsif (warnings::enabled() and length($sym) == 1 and $sym !~ tr/a-zA-Z//) { |
31 | warnings::warn("No need to declare built-in vars"); |
1d76c68a |
32 | } elsif ( $^H &= strict::bits('vars') ) { |
33 | Carp::croak("'$ch$sym' is not a valid variable name under strict vars"); |
15313f9c |
34 | } |
35 | } |
fb73857a |
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 | : do { |
43 | require Carp; |
15313f9c |
44 | Carp::croak("'$ch$sym' is not a valid variable name"); |
fb73857a |
45 | }); |
46 | } |
47 | }; |
48 | |
49 | 1; |
50 | __END__ |
51 | |
c07a80fd |
52 | =head1 NAME |
53 | |
86a9aef2 |
54 | vars - Perl pragma to predeclare global variable names (obsolete) |
c07a80fd |
55 | |
56 | =head1 SYNOPSIS |
57 | |
58 | use vars qw($frob @mung %seen); |
59 | |
60 | =head1 DESCRIPTION |
61 | |
86a9aef2 |
62 | NOTE: The functionality provided by this pragma has been superseded |
63 | by C<our> declarations, available in Perl v5.6.0 or later. See |
64 | L<perlfunc/our>. |
65 | |
c07a80fd |
66 | This will predeclare all the variables whose names are |
67 | in the list, allowing you to use them under "use strict", and |
68 | disabling any typo warnings. |
69 | |
55497cff |
70 | Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and |
71 | C<use subs> declarations are not BLOCK-scoped. They are thus effective |
72 | for the entire file in which they appear. You may not rescind such |
73 | declarations with C<no vars> or C<no subs>. |
74 | |
7a2e2cd6 |
75 | Packages such as the B<AutoLoader> and B<SelfLoader> that delay |
76 | loading of subroutines within packages can create problems with |
77 | package lexicals defined using C<my()>. While the B<vars> pragma |
78 | cannot duplicate the effect of package lexicals (total transparency |
79 | outside of the package), it can act as an acceptable substitute by |
80 | pre-declaring global symbols, ensuring their availability to the |
81 | later-loaded routines. |
c6f23971 |
82 | |
ee580363 |
83 | See L<perlmodlib/Pragmatic Modules>. |
c07a80fd |
84 | |
85 | =cut |