xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/vars.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    $ENV{PERL5LIB} = '../lib';
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate$| = 1;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gateprint "1..27\n";
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate# catch "used once" warnings
14*0Sstevel@tonic-gatemy @warns;
15*0Sstevel@tonic-gateBEGIN { $SIG{__WARN__} = sub { push @warns, @_ }; $^W = 1 };
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate%x = ();
18*0Sstevel@tonic-gate$y = 3;
19*0Sstevel@tonic-gate@z = ();
20*0Sstevel@tonic-gate$X::x = 13;
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gateuse vars qw($p @q %r *s &t $X::p);
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gatemy $e = !(grep /^Name "X::x" used only once: possible typo/, @warns) && 'not ';
25*0Sstevel@tonic-gateprint "${e}ok 1\n";
26*0Sstevel@tonic-gate$e = !(grep /^Name "main::x" used only once: possible typo/, @warns) && 'not ';
27*0Sstevel@tonic-gateprint "${e}ok 2\n";
28*0Sstevel@tonic-gate$e = !(grep /^Name "main::y" used only once: possible typo/, @warns) && 'not ';
29*0Sstevel@tonic-gateprint "${e}ok 3\n";
30*0Sstevel@tonic-gate$e = !(grep /^Name "main::z" used only once: possible typo/, @warns) && 'not ';
31*0Sstevel@tonic-gateprint "${e}ok 4\n";
32*0Sstevel@tonic-gate($e, @warns) = @warns != 4 && 'not ';
33*0Sstevel@tonic-gateprint "${e}ok 5\n";
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate# this is inside eval() to avoid creation of symbol table entries and
36*0Sstevel@tonic-gate# to avoid "used once" warnings
37*0Sstevel@tonic-gateeval <<'EOE';
38*0Sstevel@tonic-gate$e = ! $main::{p} && 'not ';
39*0Sstevel@tonic-gateprint "${e}ok 6\n";
40*0Sstevel@tonic-gate$e = ! *q{ARRAY} && 'not ';
41*0Sstevel@tonic-gateprint "${e}ok 7\n";
42*0Sstevel@tonic-gate$e = ! *r{HASH} && 'not ';
43*0Sstevel@tonic-gateprint "${e}ok 8\n";
44*0Sstevel@tonic-gate$e = ! $main::{s} && 'not ';
45*0Sstevel@tonic-gateprint "${e}ok 9\n";
46*0Sstevel@tonic-gate$e = ! *t{CODE} && 'not ';
47*0Sstevel@tonic-gateprint "${e}ok 10\n";
48*0Sstevel@tonic-gate$e = defined $X::{q} && 'not ';
49*0Sstevel@tonic-gateprint "${e}ok 11\n";
50*0Sstevel@tonic-gate$e = ! $X::{p} && 'not ';
51*0Sstevel@tonic-gateprint "${e}ok 12\n";
52*0Sstevel@tonic-gateEOE
53*0Sstevel@tonic-gate$e = $@ && 'not ';
54*0Sstevel@tonic-gateprint "${e}ok 13\n";
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gateeval q{use vars qw(@X::y !abc); $e = ! *X::y{ARRAY} && 'not '};
57*0Sstevel@tonic-gateprint "${e}ok 14\n";
58*0Sstevel@tonic-gate$e = $@ !~ /^'!abc' is not a valid variable name/ && 'not ';
59*0Sstevel@tonic-gateprint "${e}ok 15\n";
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gateeval 'use vars qw($x[3])';
62*0Sstevel@tonic-gate$e = $@ !~ /^Can't declare individual elements of hash or array/ && 'not ';
63*0Sstevel@tonic-gateprint "${e}ok 16\n";
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate{ local $^W;
66*0Sstevel@tonic-gate  eval 'use vars qw($!)';
67*0Sstevel@tonic-gate  ($e, @warns) = ($@ || @warns) ? 'not ' : '';
68*0Sstevel@tonic-gate  print "${e}ok 17\n";
69*0Sstevel@tonic-gate};
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate# NB the next test only works because vars.pm has already been loaded
72*0Sstevel@tonic-gateeval 'use warnings "vars"; use vars qw($!)';
73*0Sstevel@tonic-gate$e = ($@ || (shift(@warns)||'') !~ /^No need to declare built-in vars/)
74*0Sstevel@tonic-gate			&& 'not ';
75*0Sstevel@tonic-gateprint "${e}ok 18\n";
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateno strict 'vars';
78*0Sstevel@tonic-gateeval 'use vars qw(@x%%)';
79*0Sstevel@tonic-gate$e = $@ && 'not ';
80*0Sstevel@tonic-gateprint "${e}ok 19\n";
81*0Sstevel@tonic-gate$e = ! *{'x%%'}{ARRAY} && 'not ';
82*0Sstevel@tonic-gateprint "${e}ok 20\n";
83*0Sstevel@tonic-gateeval '$u = 3; @v = (); %w = ()';
84*0Sstevel@tonic-gate$e = $@ && 'not ';
85*0Sstevel@tonic-gateprint "${e}ok 21\n";
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gateuse strict 'vars';
88*0Sstevel@tonic-gateeval 'use vars qw(@y%%)';
89*0Sstevel@tonic-gate$e = $@ !~ /^'\@y%%' is not a valid variable name under strict vars/ && 'not ';
90*0Sstevel@tonic-gateprint "${e}ok 22\n";
91*0Sstevel@tonic-gate$e = *{'y%%'}{ARRAY} && 'not ';
92*0Sstevel@tonic-gateprint "${e}ok 23\n";
93*0Sstevel@tonic-gateeval '$u = 3; @v = (); %w = ()';
94*0Sstevel@tonic-gatemy @errs = split /\n/, $@;
95*0Sstevel@tonic-gate$e = @errs != 3 && 'not ';
96*0Sstevel@tonic-gateprint "${e}ok 24\n";
97*0Sstevel@tonic-gate$e = !(grep(/^Global symbol "\$u" requires explicit package name/, @errs))
98*0Sstevel@tonic-gate			&& 'not ';
99*0Sstevel@tonic-gateprint "${e}ok 25\n";
100*0Sstevel@tonic-gate$e = !(grep(/^Global symbol "\@v" requires explicit package name/, @errs))
101*0Sstevel@tonic-gate			&& 'not ';
102*0Sstevel@tonic-gateprint "${e}ok 26\n";
103*0Sstevel@tonic-gate$e = !(grep(/^Global symbol "\%w" requires explicit package name/, @errs))
104*0Sstevel@tonic-gate			&& 'not ';
105*0Sstevel@tonic-gateprint "${e}ok 27\n";
106