xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/vars.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage vars;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateuse 5.006;
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gateour $VERSION = '1.01';
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateuse warnings::register;
8*0Sstevel@tonic-gateuse strict qw(vars subs);
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gatesub import {
11*0Sstevel@tonic-gate    my $callpack = caller;
12*0Sstevel@tonic-gate    my ($pack, @imports) = @_;
13*0Sstevel@tonic-gate    my ($sym, $ch);
14*0Sstevel@tonic-gate    foreach (@imports) {
15*0Sstevel@tonic-gate        if (($ch, $sym) = /^([\$\@\%\*\&])(.+)/) {
16*0Sstevel@tonic-gate	    if ($sym =~ /\W/) {
17*0Sstevel@tonic-gate		# time for a more-detailed check-up
18*0Sstevel@tonic-gate		if ($sym =~ /^\w+[[{].*[]}]$/) {
19*0Sstevel@tonic-gate		    require Carp;
20*0Sstevel@tonic-gate		    Carp::croak("Can't declare individual elements of hash or array");
21*0Sstevel@tonic-gate		} elsif (warnings::enabled() and length($sym) == 1 and $sym !~ tr/a-zA-Z//) {
22*0Sstevel@tonic-gate		    warnings::warn("No need to declare built-in vars");
23*0Sstevel@tonic-gate		} elsif  (($^H &= strict::bits('vars'))) {
24*0Sstevel@tonic-gate		    require Carp;
25*0Sstevel@tonic-gate		    Carp::croak("'$_' is not a valid variable name under strict vars");
26*0Sstevel@tonic-gate		}
27*0Sstevel@tonic-gate	    }
28*0Sstevel@tonic-gate	    $sym = "${callpack}::$sym" unless $sym =~ /::/;
29*0Sstevel@tonic-gate	    *$sym =
30*0Sstevel@tonic-gate		(  $ch eq "\$" ? \$$sym
31*0Sstevel@tonic-gate		 : $ch eq "\@" ? \@$sym
32*0Sstevel@tonic-gate		 : $ch eq "\%" ? \%$sym
33*0Sstevel@tonic-gate		 : $ch eq "\*" ? \*$sym
34*0Sstevel@tonic-gate		 : $ch eq "\&" ? \&$sym
35*0Sstevel@tonic-gate		 : do {
36*0Sstevel@tonic-gate		     require Carp;
37*0Sstevel@tonic-gate		     Carp::croak("'$_' is not a valid variable name");
38*0Sstevel@tonic-gate		 });
39*0Sstevel@tonic-gate	} else {
40*0Sstevel@tonic-gate	    require Carp;
41*0Sstevel@tonic-gate	    Carp::croak("'$_' is not a valid variable name");
42*0Sstevel@tonic-gate	}
43*0Sstevel@tonic-gate    }
44*0Sstevel@tonic-gate};
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate1;
47*0Sstevel@tonic-gate__END__
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate=head1 NAME
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gatevars - Perl pragma to predeclare global variable names (obsolete)
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate=head1 SYNOPSIS
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate    use vars qw($frob @mung %seen);
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate=head1 DESCRIPTION
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gateNOTE: For variables in the current package, the functionality provided
60*0Sstevel@tonic-gateby this pragma has been superseded by C<our> declarations, available
61*0Sstevel@tonic-gatein Perl v5.6.0 or later.  See L<perlfunc/our>.
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gateThis will predeclare all the variables whose names are
64*0Sstevel@tonic-gatein the list, allowing you to use them under "use strict", and
65*0Sstevel@tonic-gatedisabling any typo warnings.
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gateUnlike pragmas that affect the C<$^H> hints variable, the C<use vars> and
68*0Sstevel@tonic-gateC<use subs> declarations are not BLOCK-scoped.  They are thus effective
69*0Sstevel@tonic-gatefor the entire file in which they appear.  You may not rescind such
70*0Sstevel@tonic-gatedeclarations with C<no vars> or C<no subs>.
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gatePackages such as the B<AutoLoader> and B<SelfLoader> that delay
73*0Sstevel@tonic-gateloading of subroutines within packages can create problems with
74*0Sstevel@tonic-gatepackage lexicals defined using C<my()>. While the B<vars> pragma
75*0Sstevel@tonic-gatecannot duplicate the effect of package lexicals (total transparency
76*0Sstevel@tonic-gateoutside of the package), it can act as an acceptable substitute by
77*0Sstevel@tonic-gatepre-declaring global symbols, ensuring their availability to the
78*0Sstevel@tonic-gatelater-loaded routines.
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gateSee L<perlmodlib/Pragmatic Modules>.
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate=cut
83