Commit | Line | Data |
---|---|---|
b9894134 | 1 | #!./perl |
2 | ||
3 | # | |
4 | # various typeglob tests | |
5 | # | |
6 | ||
9f1b1f2d GS |
7 | BEGIN { |
8 | chdir 't' if -d 't'; | |
20822f61 | 9 | @INC = '../lib'; |
9f1b1f2d GS |
10 | } |
11 | ||
12 | use warnings; | |
13 | ||
2b5e58c4 | 14 | print "1..48\n"; |
b9894134 | 15 | |
16 | # type coersion on assignment | |
17 | $foo = 'foo'; | |
18 | $bar = *main::foo; | |
19 | $bar = $foo; | |
20 | print ref(\$bar) eq 'SCALAR' ? "ok 1\n" : "not ok 1\n"; | |
21 | $foo = *main::bar; | |
22 | ||
23 | # type coersion (not) on misc ops | |
24 | ||
25 | if ($foo) { | |
26 | print ref(\$foo) eq 'GLOB' ? "ok 2\n" : "not ok 2\n"; | |
27 | } | |
28 | ||
29 | unless ($foo =~ /abcd/) { | |
30 | print ref(\$foo) eq 'GLOB' ? "ok 3\n" : "not ok 3\n"; | |
31 | } | |
32 | ||
33 | if ($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//; | |
41 | print ref(\$a) eq 'GLOB' ? "ok 5\n" : "not ok 5\n"; | |
42 | $a =~ s/^\*//; | |
43 | print $a eq 'main::foo' ? "ok 6\n" : "not ok 6\n"; | |
44 | print ref(\$b) eq 'GLOB' ? "ok 7\n" : "not ok 7\n"; | |
45 | ||
46 | # typeglobs as lvalues | |
47 | substr($foo, 0, 1) = "XXX"; | |
48 | print ref(\$foo) eq 'SCALAR' ? "ok 8\n" : "not ok 8\n"; | |
49 | print $foo eq 'XXXmain::bar' ? "ok 9\n" : "not ok 9\n"; | |
50 | ||
51 | # returning glob values | |
52 | sub foo { | |
53 | local($bar) = *main::foo; | |
54 | $foo = *main::bar; | |
55 | return ($foo, $bar); | |
56 | } | |
57 | ||
58 | ($fuu, $baa) = foo(); | |
59 | if (defined $fuu) { | |
60 | print ref(\$fuu) eq 'GLOB' ? "ok 10\n" : "not ok 10\n"; | |
61 | } | |
62 | ||
63 | if (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 |
73 | print exists $Foo::{'Bar::'} ? "ok 12\n" : "not ok 12\n"; |
74 | print $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); | |
80 | undef *foo; | |
7b8d334a | 81 | print +($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"); | |
98 | sub x { "ok 20\n" } | |
99 | print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}}; | |
f4d13ee9 JH |
100 | format x = |
101 | ok 21 | |
102 | . | |
103 | print ref *x{FORMAT} eq "FORMAT" ? "ok 21\n" : "not ok 21\n"; | |
640b9ef6 | 104 | *x = *STDOUT; |
f4d13ee9 JH |
105 | print *{*x{GLOB}} eq "*main::STDOUT" ? "ok 22\n" : "not ok 22\n"; |
106 | print {*x{IO}} "ok 23\n"; | |
39b99f21 | 107 | |
29a56bd6 JH |
108 | { |
109 | my $warn; | |
110 | local $SIG{__WARN__} = sub { | |
111 | $warn .= $_[0]; | |
112 | }; | |
113 | my $val = *x{FILEHANDLE}; | |
d1f145d3 | 114 | print {*x{IO}} ($warn =~ /is deprecated/ ? "ok 24\n" : "not ok 24\n"); |
29a56bd6 JH |
115 | |
116 | } | |
117 | ||
35cd451c GS |
118 | # test if defined() doesn't create any new symbols |
119 | ||
120 | { | |
f4d13ee9 | 121 | my $test = 24; |
35cd451c GS |
122 | |
123 | my $a = "SYM000"; | |
124 | print "not " if 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 | print "not " if defined %{$a} or defined *{$a}; | |
131 | ++$test; print "ok $test\n"; | |
132 | ||
133 | print "not " if defined ${$a} or defined *{$a}; | |
134 | ++$test; print "ok $test\n"; | |
135 | ||
136 | print "not " if defined &{$a} or defined *{$a}; | |
137 | ++$test; print "ok $test\n"; | |
138 | ||
139 | *{$a} = sub { print "ok $test\n" }; | |
140 | print "not " unless defined &{$a} and defined *{$a}; | |
141 | ++$test; &{$a}; | |
142 | } | |
640b9ef6 | 143 | |
c9d5ac95 GS |
144 | # although it *should* if you're talking about magicals |
145 | ||
146 | { | |
f4d13ee9 | 147 | my $test = 30; |
c9d5ac95 GS |
148 | |
149 | my $a = "]"; | |
150 | print "not " unless defined ${$a}; | |
151 | ++$test; print "ok $test\n"; | |
152 | print "not " unless defined *{$a}; | |
153 | ++$test; print "ok $test\n"; | |
154 | ||
155 | $a = "1"; | |
156 | "o" =~ /(o)/; | |
157 | print "not " unless ${$a}; | |
158 | ++$test; print "ok $test\n"; | |
159 | print "not " unless defined *{$a}; | |
160 | ++$test; print "ok $test\n"; | |
161 | $a = "2"; | |
162 | print "not " if ${$a}; | |
163 | ++$test; print "ok $test\n"; | |
164 | print "not " unless defined *{$a}; | |
165 | ++$test; print "ok $test\n"; | |
166 | $a = "1x"; | |
167 | print "not " if defined ${$a}; | |
168 | ++$test; print "ok $test\n"; | |
169 | print "not " if defined *{$a}; | |
170 | ++$test; print "ok $test\n"; | |
171 | $a = "11"; | |
172 | "o" =~ /(((((((((((o)))))))))))/; | |
173 | print "not " unless ${$a}; | |
174 | ++$test; print "ok $test\n"; | |
175 | print "not " unless defined *{$a}; | |
176 | ++$test; print "ok $test\n"; | |
177 | } | |
178 | ||
179 | ||
bd2155e9 JH |
180 | # [ID 20010526.001] localized glob loses value when assigned to |
181 | ||
182 | $j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{}; | |
183 | ||
184 | print $j == 1 ? "ok 41\n" : "not ok 41\n"; | |
185 | print $j{a} == 1 ? "ok 42\n" : "not ok 42\n"; | |
186 | print $j[0] == 1 ? "ok 43\n" : "not ok 43\n"; | |
187 | ||
99491443 GS |
188 | # does pp_readline() handle glob-ness correctly? |
189 | ||
190 | { | |
191 | my $g = *foo; | |
192 | $g = <DATA>; | |
193 | print $g; | |
194 | } | |
195 | ||
fb24441d RGS |
196 | { |
197 | my $w = ''; | |
198 | $SIG{__WARN__} = sub { $w = $_[0] }; | |
199 | sub abc1 (); | |
200 | local *abc1 = sub { }; | |
201 | print $w eq '' ? "ok 45\n" : "not ok 45\n# $w"; | |
202 | sub abc2 (); | |
203 | local *abc2; | |
204 | *abc2 = sub { }; | |
205 | print $w eq '' ? "ok 46\n" : "not ok 46\n# $w"; | |
206 | sub abc3 (); | |
207 | *abc3 = sub { }; | |
208 | print $w =~ /Prototype mismatch/ ? "ok 47\n" : "not ok 47\n# $w"; | |
209 | } | |
210 | ||
2b5e58c4 AMS |
211 | { |
212 | # [17375] rcatline to formerly-defined undef was broken. Fixed in | |
213 | # do_readline by checking SvOK. AMS, 20020918 | |
214 | my $x = "not "; | |
215 | $x = undef; | |
216 | $x .= <DATA>; | |
217 | print $x; | |
218 | } | |
219 | ||
99491443 | 220 | __END__ |
bd2155e9 | 221 | ok 44 |
2b5e58c4 | 222 | ok 48 |