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