This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20020422.003] Suggestion in Perl 5.6.1 installation on AIX
[perl5.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         # 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.
19         ($ch, $sym) = unpack('a1a*', $_);
20         if ($sym =~ tr/A-Za-z_0-9//c) {
21             # time for a more-detailed check-up
22             if ($sym =~ /^\w+[[{].*[]}]$/) {
23                 require Carp;
24                 Carp::croak("Can't declare individual elements of hash or array");
25             } elsif (warnings::enabled() and length($sym) == 1 and $sym !~ tr/a-zA-Z//) {
26                 warnings::warn("No need to declare built-in vars");
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.
30                 require Carp;
31                 Carp::croak("'$_' is not a valid variable name under strict vars");
32             }
33         }
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
41            : do {
42                 require Carp;
43                 Carp::croak("'$_' is not a valid variable name");
44              });
45     }
46 };
47
48 1;
49 __END__
50
51 =head1 NAME
52
53 vars - Perl pragma to predeclare global variable names (obsolete)
54
55 =head1 SYNOPSIS
56
57     use vars qw($frob @mung %seen);
58
59 =head1 DESCRIPTION
60
61 NOTE: For variables in the current package, the functionality provided
62 by this pragma has been superseded by C<our> declarations, available
63 in Perl v5.6.0 or later.  See L<perlfunc/our>.
64
65 This will predeclare all the variables whose names are 
66 in the list, allowing you to use them under "use strict", and
67 disabling any typo warnings.
68
69 Unlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
70 C<use subs> declarations are not BLOCK-scoped.  They are thus effective
71 for the entire file in which they appear.  You may not rescind such
72 declarations with C<no vars> or C<no subs>.
73
74 Packages such as the B<AutoLoader> and B<SelfLoader> that delay
75 loading of subroutines within packages can create problems with
76 package lexicals defined using C<my()>. While the B<vars> pragma
77 cannot duplicate the effect of package lexicals (total transparency
78 outside of the package), it can act as an acceptable substitute by
79 pre-declaring global symbols, ensuring their availability to the
80 later-loaded routines.
81
82 The C<use vars> does not work for UTF-8 variable names.
83
84 See L<perlmodlib/Pragmatic Modules>.
85
86 =cut