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