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