Re: t/op/tie.t #19 TODO ENOTWORKING
[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) {
82686b01 15 # TODO: UTF-8 names: (the unpack is quite wrong,
16 # /^(.)(.*)/ would probably be better.) While you
17 # are at it, until declaring empty package is made
18 # to work the * is too lenient.
7eb43e02 19 ($ch, $sym) = unpack('a1a*', $_);
014aafb8 20 if ($sym =~ tr/A-Za-z_0-9//c) {
15313f9c 21 # time for a more-detailed check-up
7eb43e02 22 if ($sym =~ /^\w+[[{].*[]}]$/) {
15313f9c 23 require Carp;
24 Carp::croak("Can't declare individual elements of hash or array");
d3a7d8c7 25 } elsif (warnings::enabled() and length($sym) == 1 and $sym !~ tr/a-zA-Z//) {
26 warnings::warn("No need to declare built-in vars");
82686b01 27 } elsif (($^H &= strict::bits('vars'))) {
28 # TODO: UTF-8 names: be careful to load the UTF-8
29 # machinery only if the symbol requires it.
13368207 30 require Carp;
31 Carp::croak("'$_' is not a valid variable name under strict vars");
15313f9c 32 }
33 }
7eb43e02 34 $sym = "${callpack}::$sym" unless $sym =~ /::/;
35 *$sym =
36 ( $ch eq "\$" ? \$$sym
37 : $ch eq "\@" ? \@$sym
38 : $ch eq "\%" ? \%$sym
39 : $ch eq "\*" ? \*$sym
40 : $ch eq "\&" ? \&$sym
fb73857a 41 : do {
42 require Carp;
7eb43e02 43 Carp::croak("'$_' is not a valid variable name");
fb73857a 44 });
45 }
46};
47
481;
49__END__
50
c07a80fd 51=head1 NAME
52
86a9aef2 53vars - Perl pragma to predeclare global variable names (obsolete)
c07a80fd 54
55=head1 SYNOPSIS
56
57 use vars qw($frob @mung %seen);
58
59=head1 DESCRIPTION
60
7eb43e02 61NOTE: For variables in the current package, the functionality provided
62by this pragma has been superseded by C<our> declarations, available
63in Perl v5.6.0 or later. See L<perlfunc/our>.
86a9aef2 64
c07a80fd 65This will predeclare all the variables whose names are
66in the list, allowing you to use them under "use strict", and
67disabling any typo warnings.
68
55497cff 69Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
70C<use subs> declarations are not BLOCK-scoped. They are thus effective
71for the entire file in which they appear. You may not rescind such
72declarations with C<no vars> or C<no subs>.
73
7a2e2cd6 74Packages such as the B<AutoLoader> and B<SelfLoader> that delay
75loading of subroutines within packages can create problems with
76package lexicals defined using C<my()>. While the B<vars> pragma
77cannot duplicate the effect of package lexicals (total transparency
78outside of the package), it can act as an acceptable substitute by
79pre-declaring global symbols, ensuring their availability to the
80later-loaded routines.
c6f23971 81
c20e2abd 82The C<use vars> does not work for UTF-8 variable names.
83
ee580363 84See L<perlmodlib/Pragmatic Modules>.
c07a80fd 85
86=cut