5 this file contains all dereferencing tests from ref.t but using postfix instead of prefix or circumfix syntax.
17 use strict qw(refs subs);
23 # Test fake references.
28 # is ($$$foo, 'valid');
29 is ($$foo->$*, 'valid');
30 is ($foo->$*->$*, 'valid');
33 # Test real references.
39 is ($$FOO ->$*, 'hit');
40 is ($FOO-> $* ->$*, 'hit');
42 # Test references to real arrays.
44 my $test = curr_test();
45 @ary = ($test,$test+1,$test+2,$test+3);
51 # push(@{$ref[$i]}, "ok $ary[$i]\n");
52 push($ref[$i]-> @*, "ok $ary[$i]\n");
61 print 'd'->@*; # print @{'d'};
65 # Test references to references.
69 is ($refref->$*->$*, 'Good'); # is ($$$refref, 'Good');
72 # Test nested anonymous arrays.
74 $ref = [[],2,[3,4,5,]];
75 is (scalar $ref->@*, 3); # is (scalar @$ref, 3);
76 is ($ref->[1], 2); # is ($$ref[1], 2);
77 # is (${$$ref[2]}[2], 5);
78 is (${$ref->[2]}[2], 5);
79 is ($ref->[2]->[2], 5);
81 is (scalar $ref->[0]->@*, 0); # is (scalar @{$$ref[0]}, 0);
84 is ($ref->[2]->[0], 3);
86 # Test references to hashes of references.
89 $refref->{"key"} = $ref;
90 is ($refref->{"key"}->[2]->[0], 3);
91 is ($refref->{"key"}->[2][0], 3);
92 is ($refref->{"key"}[2]->[0], 3);
93 is ($refref->{"key"}[2][0], 3);
95 # Test to see if anonymous subarrays spring into existence.
97 $spring[5]->[0] = 123;
98 $spring[5]->[1] = 456;
99 push($spring[5]->@*, 789); # push(@{$spring[5]}, 789);
100 is (join(':',$spring[5]->@*), "123:456:789"); # is (join(':',@{$spring[5]}), "123:456:789");
102 # Test to see if anonymous subhashes spring into existence.
104 $spring2{"foo"}->@* = (1,2,3); # @{$spring2{"foo"}} = (1,2,3);
105 $spring2{"foo"}->[3] = 4;
106 is (join(':',$spring2{"foo"}->@*), "1:2:3:4");
108 # Test references to subroutines.
112 sub mysub { $called++; }
113 local $subref = \&mysub;
116 ok(eval '$subref->&*',"ampersand-star runs coderef: syntax");
119 ok(eval q{'mysubalias'->** = 'mysub'->**->*{CODE}}, "glob access syntax");
120 is ( eval 'mysubalias()', 2);
124 is ref eval {\&{""}}, "CODE", 'reference to &{""} [perl #94476]';
126 # Test references to return values of operators (TARGs/PADTMPs)
132 # is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP';
133 is join(" ", map $_->$*, @refs), "a b", 'refgen+PADTMP';
136 $subrefref = \\&mysub2;
137 is ($subrefref->$*->("GOOD"), "good"); # is ($$subrefref->("GOOD"), "good");
138 sub mysub2 { lc shift }
141 # Test anonymous hash syntax.
144 is (ref $anonhash, 'HASH');
145 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
146 is (join('', sort values $anonhash2->%*), 'BARXYZ'); # is (join('', sort values %$anonhash2), 'BARXYZ');
147 $anonhash2->{23} = 'tt';@$anonhash2{skiddoo=> 99} = qw/rr nn/;
148 is(join(':',$anonhash2->@{23 => skiddoo => 99}), 'tt:rr:nn', 'pf hash slice');
150 # test immediate destruction of lexical objects (op/ref.t tests LIFO order)
151 { my $test = curr_test();
152 my ($ScopeMark, $Stoogetime) = (1,$test);
153 sub InScope() { $ScopeMark ? "ok " : "not ok " }
154 sub shoulda::DESTROY { print InScope,$test++," - Larry\n"; }
155 sub coulda::DESTROY { print InScope,$test++," - Curly\n"; }
156 sub woulda::DESTROY { print InScope,$test++," - Moe\n"; }
157 sub frieda::DESTROY { print InScope,$test++," - Shemp\n"; }
158 sub spr::DESTROY { print InScope,$test++," - postfix scalar reference\n"; }
159 sub apr::DESTROY { print InScope,$test++," - postfix array reference\n"; }
160 sub hpr::DESTROY { print InScope,$test++," - postfix hash reference\n"; }
164 # and real references taken from symbolic postfix dereferences
165 local ($joe, @curly, %larry, $momo);
167 my $woulda = bless \'joe'->$*, 'woulda';
168 my $frieda = bless \'momo'->$*, 'frieda';
169 my $coulda = eval q{bless \'curly'->@*, 'coulda' } or print "# $@","not ok ",$test++,"\n";
170 my $shoulda = eval q{bless \'larry'->%*, 'shoulda'} or print "# $@","not ok ",$test++,"\n";
171 # print "# postfix whack-star instead of prefix whack\n";
172 # my $spr = eval q/ bless $s->\* , "spr"/; $@ and print "# $@","not ok ",$test++,"\n";
173 # my $apr = eval q/ bless @a->\* , 'apr'/; $@ and print "# $@","not ok ",$test++,"\n";
174 # my $hpr = eval q/ bless %h->\* , 'hpr'/; $@ and print "# $@","not ok ",$test++,"\n";
175 print "# leaving block: we want (larry, curly, moe, shemp)\n";
178 print "# left block\n";
181 is ($test, $Stoogetime + 4, "no stooges outlast their scope");
187 $name_utf8 = $name8 . chr 256;
190 # is ($$name8, undef, 'Nothing before we start');
191 # is ($$name_utf8, undef, 'Nothing before we start');
193 # is ($$name8, "Pound", 'Accessing via 8 bit symref works');
194 # is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
196 is ($name8->$*, undef, 'Nothing before we start');
197 is ($name_utf8->$*, undef, 'Nothing before we start');
198 $name8->$* = "Pound";
199 is ($name8->$*, "Pound", 'Accessing via 8 bit symref works');
200 is ($name_utf8->$*, "Pound", 'Accessing via UTF8 symref works');
205 $name_utf8 = $name = chr 9787;
206 utf8::encode $name_utf8;
208 is (length $name, 1, "Name is 1 char");
209 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
211 is ($name->$*, undef, 'Nothing before we start');
212 is ($name_utf8->$*, undef, 'Nothing before we start');
214 is ($name->$*, "Face", 'Accessing via Unicode symref works');
215 is ($name_utf8->$*, undef,
216 'Accessing via the UTF8 byte sequence still gives nothing');
224 is ($ $name1, undef, 'Nothing before we start (scalars)');
225 is ($name2 -> $* , undef, 'Nothing before we start');
226 $name1 ->$* = "Yummy";
227 is ($name1-> $*, "Yummy", 'Accessing via the correct name works');
229 'Accessing via a different NUL-containing name gives nothing');
230 # defined uses a different code path
231 ok (defined $name1->$*, 'defined via the correct name works');
232 ok (!defined $name2->$*,
233 'defined via a different NUL-containing name gives nothing');
235 my (undef, $one) = $name1 ->@[2,3];
236 my (undef, $two) = $name2-> @[2,3];
237 is ($one, undef, 'Nothing before we start (array slices)');
238 is ($two, undef, 'Nothing before we start');
239 $name1->@[2,3] = ("Very", "Yummy");
240 (undef, $one) = $name1 -> @[2,3];
241 (undef, $two) = $name2 -> @[2,3];
242 is ($one, "Yummy", 'Accessing via the correct name works');
244 'Accessing via a different NUL-containing name gives nothing');
245 ok (defined $one, 'defined via the correct name works');
247 'defined via a different NUL-containing name gives nothing');
252 # test dereferencing errors
257 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
259 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
261 like($@, qr/Not an ARRAY reference/, "Array dereference");
263 like($@, qr/Not a HASH reference/, "Hash dereference");
265 like($@, qr/Not a CODE reference/, "Code dereference");
268 $ref = *STDERR{FORMAT};
269 eval q/ $ref->** /; # postfix GLOB dereference ?
270 like($@, qr/Not a GLOB reference/, "Glob dereference");
274 is($@, '', "Glob dereference of PVIO is acceptable");
276 is($ref, (eval '$ref->*{IO}'), "IO slot of the temporary glob is set correctly");
279 # these will segfault if they fail
280 sub PVBM () { 'foo' }
282 ok(eval q/ $pvbm_r = \'PVBM'->&* /, "postfix symref to sub name");
283 is("$pvbm_r", "".\&PVBM, "postfix and prefix mechanisms provide same result");
288 ok (!eval q{ $SynCtr++; $rpvbm->** }, 'PVBM ref is not a GLOB ref');
289 ok (!eval q{ $SynCtr++; $pvbm->** }, 'PVBM is not a GLOB ref');
290 is ($SynCtr, 2, "starstar GLOB postderef parses");
292 ok (!eval { $pvbm->$* }, 'PVBM is not a SCALAR ref');
293 ok (!eval { $pvbm->@* }, 'PVBM is not an ARRAY ref');
294 ok (!eval { $pvbm->%* }, 'PVBM is not a HASH ref');
296 # Test undefined hash references as arguments to %{} in boolean context
300 eval { my $foo; $foo->%*; }; ok !$@, '%$undef';
301 eval { my $foo; scalar $foo->%*; }; ok !$@, 'scalar %$undef';
302 eval { my $foo; !$foo->%*; }; ok !$@, '!%$undef';
303 eval { my $foo; if ( $foo->%*) {} }; ok !$@, 'if ( %$undef) {}';
304 eval { my $foo; if (!$foo->%*) {} }; ok !$@, 'if (!%$undef) {}';
305 eval { my $foo; unless ( $foo->%*) {} }; ok !$@, 'unless ( %$undef) {}';
306 eval { my $foo; unless (!$foo->%*) {} }; ok !$@, 'unless (!%$undef) {}';
307 eval { my $foo; 1 if $foo->%*; }; ok !$@, '1 if %$undef';
308 eval { my $foo; 1 if !$foo->%*; }; ok !$@, '1 if !%$undef';
309 eval { my $foo; 1 unless $foo->%*; }; ok !$@, '1 unless %$undef;';
310 eval { my $foo; 1 unless ! $foo->%*; }; ok !$@, '1 unless ! %$undef';
311 eval { my $foo; $foo->%* ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0';
312 eval { my $foo; !$foo->%* ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0';
315 # Postfix key/value slices
316 is join(" ", {1..10}->%{1, 7, 3}), "1 2 7 8 3 4", '->%{';
317 is join(" ", ['a'..'z']->%[1, 7, 3]), "1 b 7 h 3 d", '->%[';
320 is [1..10]->$#*, 9, 'rvalue ->$#*';
323 is "@foo", "1 2 3 4 5 6 7 8 9", 'lvalue ->$#*';
328 %foo = qw( foo oof );
330 no warnings 'deprecated';
332 is "$_->$*", 'foo->42', '->$* interpolation without feature';
334 is "$_->$#*", 'foo->43*', '->$#* interpolation without feature';
336 is "$_->@*", 'foo->@*', '->@* does not interpolate without feature';
337 is "$_->@[0]", 'foo->@[0]', '->@[ does not interpolate without feature';
338 is "$_->@{foo}", "foo->7 8 9", '->@{ does not interpolate without feature';
340 use feature 'postderef_qq';
343 is "$_->$*", "43", '->$* interpolated';
344 is "$_->$#*", "2", '->$#* interpolated';
345 is "$_->@*", "7 8 9", '->@* interpolated';
346 is "$_->@[0,1]", "7 8", '->@[ interpolated';
347 is "$_->@{foo}", "oof", '->@{ interpolated';
348 is "foo$_->$*bar", "foo43bar", '->$* interpolated w/other stuff';
349 is "foo$_->@*bar", "foo7 8 9bar", '->@* interpolated w/other stuff';
350 is "foo$_->@[0,1]bar", "foo7 8bar", '->@[ interpolated w/other stuff';
351 is "foo$_->@{foo}bar", "foooofbar", '->@{ interpolated w/other stuff';
352 is "@{[foo->@*]}", "7 8 9", '->@* inside "@{...}"';
353 is "@{[foo->@[0,1]]}", "7 8", '->@[ inside "@{...}"';
354 is "@{[foo->@{foo}]}", "oof", '->@{ inside "@{...}"';