Commit | Line | Data |
c07a80fd |
1 | package vars; |
2 | |
3 | =head1 NAME |
4 | |
5 | vars - Perl pragma to predeclare global variable names |
6 | |
7 | =head1 SYNOPSIS |
8 | |
9 | use vars qw($frob @mung %seen); |
10 | |
11 | =head1 DESCRIPTION |
12 | |
13 | This will predeclare all the variables whose names are |
14 | in the list, allowing you to use them under "use strict", and |
15 | disabling any typo warnings. |
16 | |
17 | See L<perlmod/Pragmatic Modules>. |
18 | |
19 | =cut |
20 | require 5.000; |
21 | use Carp; |
22 | |
23 | sub import { |
24 | my $callpack = caller; |
25 | my ($pack, @imports, $sym, $ch) = @_; |
26 | foreach $sym (@imports) { |
27 | croak "Can't declare another package's variables" if $sym =~ /::/; |
28 | ($ch, $sym) = unpack('a1a*', $sym); |
29 | *{"${callpack}::$sym"} = |
30 | ( $ch eq "\$" ? \$ {"${callpack}::$sym"} |
31 | : $ch eq "\@" ? \@ {"${callpack}::$sym"} |
32 | : $ch eq "\%" ? \% {"${callpack}::$sym"} |
33 | : $ch eq "\*" ? \* {"${callpack}::$sym"} |
34 | : $ch eq "\&" ? \& {"${callpack}::$sym"} |
35 | : croak "'$ch$sym' is not a valid variable name\n"); |
36 | } |
37 | }; |
38 | |
39 | 1; |