5 vars - Perl pragma to predeclare global variable names
9 use vars qw($frob @mung %seen);
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.
17 Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
18 C<use subs> declarations are not BLOCK-scoped. They are thus effective
19 for the entire file in which they appear. You may not rescind such
20 declarations with C<no vars> or C<no subs>.
22 Packages such as the B<AutoLoader> and B<SelfLoader> that delay loading
23 of subroutines within packages can create problems with package lexicals
24 defined using C<my()>. While the B<vars> pragma cannot duplicate the
25 effect of package lexicals (total transparency outside of the package),
26 it can act as an acceptable substitute by pre-declaring global symbols,
27 ensuring their availability to to the later-loaded routines.
29 See L<perlmod/Pragmatic Modules>.
37 my $callpack = caller;
38 my ($pack, @imports, $sym, $ch) = @_;
39 foreach $sym (@imports) {
40 croak "Can't declare another package's variables" if $sym =~ /::/;
41 ($ch, $sym) = unpack('a1a*', $sym);
42 *{"${callpack}::$sym"} =
43 ( $ch eq "\$" ? \$ {"${callpack}::$sym"}
44 : $ch eq "\@" ? \@ {"${callpack}::$sym"}
45 : $ch eq "\%" ? \% {"${callpack}::$sym"}
46 : $ch eq "\*" ? \* {"${callpack}::$sym"}
47 : $ch eq "\&" ? \& {"${callpack}::$sym"}
48 : croak "'$ch$sym' is not a valid variable name\n");