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