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