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