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