This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.git] / t / op / gv.t
CommitLineData
b9894134 1#!./perl
2
3#
4# various typeglob tests
5#
6
9f1b1f2d
GS
7BEGIN {
8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
9f1b1f2d
GS
10}
11
12use warnings;
13
f4d13ee9 14print "1..41\n";
b9894134 15
16# type coersion on assignment
17$foo = 'foo';
18$bar = *main::foo;
19$bar = $foo;
20print ref(\$bar) eq 'SCALAR' ? "ok 1\n" : "not ok 1\n";
21$foo = *main::bar;
22
23# type coersion (not) on misc ops
24
25if ($foo) {
26 print ref(\$foo) eq 'GLOB' ? "ok 2\n" : "not ok 2\n";
27}
28
29unless ($foo =~ /abcd/) {
30 print ref(\$foo) eq 'GLOB' ? "ok 3\n" : "not ok 3\n";
31}
32
33if ($foo eq '*main::bar') {
34 print ref(\$foo) eq 'GLOB' ? "ok 4\n" : "not ok 4\n";
35}
36
37# type coersion on substitutions that match
38$a = *main::foo;
39$b = $a;
40$a =~ s/^X//;
41print ref(\$a) eq 'GLOB' ? "ok 5\n" : "not ok 5\n";
42$a =~ s/^\*//;
43print $a eq 'main::foo' ? "ok 6\n" : "not ok 6\n";
44print ref(\$b) eq 'GLOB' ? "ok 7\n" : "not ok 7\n";
45
46# typeglobs as lvalues
47substr($foo, 0, 1) = "XXX";
48print ref(\$foo) eq 'SCALAR' ? "ok 8\n" : "not ok 8\n";
49print $foo eq 'XXXmain::bar' ? "ok 9\n" : "not ok 9\n";
50
51# returning glob values
52sub foo {
53 local($bar) = *main::foo;
54 $foo = *main::bar;
55 return ($foo, $bar);
56}
57
58($fuu, $baa) = foo();
59if (defined $fuu) {
60 print ref(\$fuu) eq 'GLOB' ? "ok 10\n" : "not ok 10\n";
61}
62
63if (defined $baa) {
64 print ref(\$baa) eq 'GLOB' ? "ok 11\n" : "not ok 11\n";
65}
66
85aff577
CS
67# nested package globs
68# NOTE: It's probably OK if these semantics change, because the
69# fact that %X::Y:: is stored in %X:: isn't documented.
70# (I hope.)
71
9f1b1f2d 72{ package Foo::Bar; no warnings 'once'; $test=1; }
85aff577
CS
73print exists $Foo::{'Bar::'} ? "ok 12\n" : "not ok 12\n";
74print $Foo::{'Bar::'} eq '*Foo::Bar::' ? "ok 13\n" : "not ok 13\n";
20408e3c
GS
75
76# test undef operator clearing out entire glob
77$foo = 'stuff';
78@foo = qw(more stuff);
79%foo = qw(even more random stuff);
80undef *foo;
7b8d334a 81print +($foo || @foo || %foo) ? "not ok" : "ok", " 14\n";
20408e3c
GS
82
83# test warnings from assignment of undef to glob
84{
85 my $msg;
86 local $SIG{__WARN__} = sub { $msg = $_[0] };
9f1b1f2d 87 use warnings;
20408e3c 88 *foo = 'bar';
7b8d334a 89 print $msg ? "not ok" : "ok", " 15\n";
20408e3c 90 *foo = undef;
7b8d334a 91 print $msg ? "ok" : "not ok", " 16\n";
20408e3c 92}
640b9ef6
SM
93
94# test *glob{THING} syntax
95$x = "ok 17\n";
96@x = ("ok 18\n");
97%x = ("ok 19" => "\n");
98sub x { "ok 20\n" }
99print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}};
f4d13ee9
JH
100format x =
101ok 21
102.
103print ref *x{FORMAT} eq "FORMAT" ? "ok 21\n" : "not ok 21\n";
640b9ef6 104*x = *STDOUT;
f4d13ee9
JH
105print *{*x{GLOB}} eq "*main::STDOUT" ? "ok 22\n" : "not ok 22\n";
106print {*x{IO}} "ok 23\n";
107print {*x{FILEHANDLE}} "ok 24\n";
640b9ef6 108
35cd451c
GS
109# test if defined() doesn't create any new symbols
110
111{
f4d13ee9 112 my $test = 24;
35cd451c
GS
113
114 my $a = "SYM000";
115 print "not " if defined *{$a};
116 ++$test; print "ok $test\n";
117
118 print "not " if defined @{$a} or defined *{$a};
119 ++$test; print "ok $test\n";
120
121 print "not " if defined %{$a} or defined *{$a};
122 ++$test; print "ok $test\n";
123
124 print "not " if defined ${$a} or defined *{$a};
125 ++$test; print "ok $test\n";
126
127 print "not " if defined &{$a} or defined *{$a};
128 ++$test; print "ok $test\n";
129
130 *{$a} = sub { print "ok $test\n" };
131 print "not " unless defined &{$a} and defined *{$a};
132 ++$test; &{$a};
133}
640b9ef6 134
c9d5ac95
GS
135# although it *should* if you're talking about magicals
136
137{
f4d13ee9 138 my $test = 30;
c9d5ac95
GS
139
140 my $a = "]";
141 print "not " unless defined ${$a};
142 ++$test; print "ok $test\n";
143 print "not " unless defined *{$a};
144 ++$test; print "ok $test\n";
145
146 $a = "1";
147 "o" =~ /(o)/;
148 print "not " unless ${$a};
149 ++$test; print "ok $test\n";
150 print "not " unless defined *{$a};
151 ++$test; print "ok $test\n";
152 $a = "2";
153 print "not " if ${$a};
154 ++$test; print "ok $test\n";
155 print "not " unless defined *{$a};
156 ++$test; print "ok $test\n";
157 $a = "1x";
158 print "not " if defined ${$a};
159 ++$test; print "ok $test\n";
160 print "not " if defined *{$a};
161 ++$test; print "ok $test\n";
162 $a = "11";
163 "o" =~ /(((((((((((o)))))))))))/;
164 print "not " unless ${$a};
165 ++$test; print "ok $test\n";
166 print "not " unless defined *{$a};
167 ++$test; print "ok $test\n";
168}
169
170
99491443
GS
171# does pp_readline() handle glob-ness correctly?
172
173{
174 my $g = *foo;
175 $g = <DATA>;
176 print $g;
177}
178
179__END__
f4d13ee9 180ok 41