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);
22 no warnings qw 'deprecated syntax';
24 like $@, qr/Can't call method/, '->$* outside of feature scope';
26 like $@, qr/syntax error/, '->@* outside of feature scope';
28 like $@, qr/syntax error/, '->@[ outside of feature scope';
30 like $@, qr/syntax error/, '->@{ outside of feature scope';
32 like $@, qr/syntax error/, '->%* outside of feature scope';
34 like $@, qr/syntax error/, '->%[ outside of feature scope';
36 like $@, qr/syntax error/, '->%{ outside of feature scope';
38 like $@, qr/syntax error/, '->&* outside of feature scope';
40 like $@, qr/syntax error/, '->** outside of feature scope';
42 like $@, qr/syntax error/, '->*{ outside of feature scope';
45 use feature 'postderef';
46 no warnings 'experimental::postderef';
50 # Test fake references.
55 # is ($$$foo, 'valid');
56 is ($$foo->$*, 'valid');
57 is ($foo->$*->$*, 'valid');
60 # Test real references.
66 is ($$FOO ->$*, 'hit');
67 is ($FOO-> $* ->$*, 'hit');
69 # Test references to real arrays.
71 my $test = curr_test();
72 @ary = ($test,$test+1,$test+2,$test+3);
78 # push(@{$ref[$i]}, "ok $ary[$i]\n");
79 push($ref[$i]-> @*, "ok $ary[$i]\n");
88 print 'd'->@*; # print @{'d'};
92 # Test references to references.
96 is ($refref->$*->$*, 'Good'); # is ($$$refref, 'Good');
99 # Test nested anonymous lists.
101 $ref = [[],2,[3,4,5,]];
102 is (scalar $ref->@*, 3); # is (scalar @$ref, 3);
103 is ($ref->[1], 2); # is ($$ref[1], 2);
104 # is (${$$ref[2]}[2], 5);
105 is (${$ref->[2]}[2], 5);
106 is ($ref->[2]->[2], 5);
107 is ($ref->[2][2], 5);
108 is (scalar $ref->[0]->@*, 0); # is (scalar @{$$ref[0]}, 0);
111 is ($ref->[2]->[0], 3);
113 # Test references to hashes of references.
115 $refref = \%whatever;
116 $refref->{"key"} = $ref;
117 is ($refref->{"key"}->[2]->[0], 3);
118 is ($refref->{"key"}->[2][0], 3);
119 is ($refref->{"key"}[2]->[0], 3);
120 is ($refref->{"key"}[2][0], 3);
122 # Test to see if anonymous subarrays spring into existence.
124 $spring[5]->[0] = 123;
125 $spring[5]->[1] = 456;
126 push($spring[5]->@*, 789); # push(@{$spring[5]}, 789);
127 is (join(':',$spring[5]->@*), "123:456:789"); # is (join(':',@{$spring[5]}), "123:456:789");
129 # Test to see if anonymous subhashes spring into existence.
131 $spring2{"foo"}->@* = (1,2,3); # @{$spring2{"foo"}} = (1,2,3);
132 $spring2{"foo"}->[3] = 4;
133 is (join(':',$spring2{"foo"}->@*), "1:2:3:4");
135 # Test references to subroutines.
139 sub mysub { $called++; }
140 local $subref = \&mysub;
143 ok(eval '$subref->&*',"ampersand-star runs coderef: syntax");
146 ok(eval q{'mysubalias'->** = 'mysub'->**->*{CODE}}, "glob access syntax");
147 is ( eval 'mysubalias()', 2);
151 is ref eval {\&{""}}, "CODE", 'reference to &{""} [perl #94476]';
153 # Test references to return values of operators (TARGs/PADTMPs)
159 # is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP';
160 is join(" ", map $_->$*, @refs), "a b", 'refgen+PADTMP';
163 $subrefref = \\&mysub2;
164 is ($subrefref->$*->("GOOD"), "good"); # is ($$subrefref->("GOOD"), "good");
165 sub mysub2 { lc shift }
168 # Test anonymous hash syntax.
171 is (ref $anonhash, 'HASH');
172 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
173 is (join('', sort values $anonhash2->%*), 'BARXYZ'); # is (join('', sort values %$anonhash2), 'BARXYZ');
174 $anonhash2->{23} = 'tt';@$anonhash2{skiddoo=> 99} = qw/rr nn/;
175 is(join(':',$anonhash2->@{23 => skiddoo => 99}), 'tt:rr:nn', 'pf hash slice');
177 # test immediate destruction of lexical objects (op/ref.t tests LIFO order)
178 { my $test = curr_test();
179 my ($ScopeMark, $Stoogetime) = (1,$test);
180 sub InScope() { $ScopeMark ? "ok " : "not ok " }
181 sub shoulda::DESTROY { print InScope,$test++," - Larry\n"; }
182 sub coulda::DESTROY { print InScope,$test++," - Curly\n"; }
183 sub woulda::DESTROY { print InScope,$test++," - Moe\n"; }
184 sub frieda::DESTROY { print InScope,$test++," - Shemp\n"; }
185 sub spr::DESTROY { print InScope,$test++," - postfix scalar reference\n"; }
186 sub apr::DESTROY { print InScope,$test++," - postfix array reference\n"; }
187 sub hpr::DESTROY { print InScope,$test++," - postfix hash reference\n"; }
191 # and real references taken from symbolic postfix dereferences
192 local ($joe, @curly, %larry, $momo);
194 my $woulda = bless \'joe'->$*, 'woulda';
195 my $frieda = bless \'momo'->$*, 'frieda';
196 my $coulda = eval q{bless \'curly'->@*, 'coulda' } or print "# $@","not ok ",$test++,"\n";
197 my $shoulda = eval q{bless \'larry'->%*, 'shoulda'} or print "# $@","not ok ",$test++,"\n";
198 # print "# postfix whack-star instead of prefix whack\n";
199 # my $spr = eval q/ bless $s->\* , "spr"/; $@ and print "# $@","not ok ",$test++,"\n";
200 # my $apr = eval q/ bless @a->\* , 'apr'/; $@ and print "# $@","not ok ",$test++,"\n";
201 # my $hpr = eval q/ bless %h->\* , 'hpr'/; $@ and print "# $@","not ok ",$test++,"\n";
202 print "# leaving block: we want (larry, curly, moe, shemp)\n";
205 print "# left block\n";
208 is ($test, $Stoogetime + 4, "no stooges outlast their scope");
214 $name_utf8 = $name8 . chr 256;
217 # is ($$name8, undef, 'Nothing before we start');
218 # is ($$name_utf8, undef, 'Nothing before we start');
220 # is ($$name8, "Pound", 'Accessing via 8 bit symref works');
221 # is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
223 is ($name8->$*, undef, 'Nothing before we start');
224 is ($name_utf8->$*, undef, 'Nothing before we start');
225 $name8->$* = "Pound";
226 is ($name8->$*, "Pound", 'Accessing via 8 bit symref works');
227 is ($name_utf8->$*, "Pound", 'Accessing via UTF8 symref works');
232 $name_utf8 = $name = chr 9787;
233 utf8::encode $name_utf8;
235 is (length $name, 1, "Name is 1 char");
236 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
238 is ($name->$*, undef, 'Nothing before we start');
239 is ($name_utf8->$*, undef, 'Nothing before we start');
241 is ($name->$*, "Face", 'Accessing via Unicode symref works');
242 is ($name_utf8->$*, undef,
243 'Accessing via the UTF8 byte sequence still gives nothing');
251 is ($ $name1, undef, 'Nothing before we start (scalars)');
252 is ($name2 -> $* , undef, 'Nothing before we start');
253 $name1 ->$* = "Yummy";
254 is ($name1-> $*, "Yummy", 'Accessing via the correct name works');
256 'Accessing via a different NUL-containing name gives nothing');
257 # defined uses a different code path
258 ok (defined $name1->$*, 'defined via the correct name works');
259 ok (!defined $name2->$*,
260 'defined via a different NUL-containing name gives nothing');
262 my (undef, $one) = $name1 ->@[2,3];
263 my (undef, $two) = $name2-> @[2,3];
264 is ($one, undef, 'Nothing before we start (array slices)');
265 is ($two, undef, 'Nothing before we start');
266 $name1->@[2,3] = ("Very", "Yummy");
267 (undef, $one) = $name1 -> @[2,3];
268 (undef, $two) = $name2 -> @[2,3];
269 is ($one, "Yummy", 'Accessing via the correct name works');
271 'Accessing via a different NUL-containing name gives nothing');
272 ok (defined $one, 'defined via the correct name works');
274 'defined via a different NUL-containing name gives nothing');
279 # test dereferencing errors
284 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
286 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
288 like($@, qr/Not an ARRAY reference/, "Array dereference");
290 like($@, qr/Not a HASH reference/, "Hash dereference");
292 like($@, qr/Not a CODE reference/, "Code dereference");
295 $ref = *STDERR{FORMAT};
296 eval q/ $ref->** /; # postfix GLOB dereference ?
297 like($@, qr/Not a GLOB reference/, "Glob dereference");
301 is($@, '', "Glob dereference of PVIO is acceptable");
303 is($ref, (eval '$ref->*{IO}'), "IO slot of the temporary glob is set correctly");
306 # these will segfault if they fail
307 sub PVBM () { 'foo' }
309 ok(eval q/ $pvbm_r = \'PVBM'->&* /, "postfix symref to sub name");
310 is("$pvbm_r", "".\&PVBM, "postfix and prefix mechanisms provide same result");
315 ok (!eval q{ $SynCtr++; $rpvbm->** }, 'PVBM ref is not a GLOB ref');
316 ok (!eval q{ $SynCtr++; $pvbm->** }, 'PVBM is not a GLOB ref');
317 is ($SynCtr, 2, "starstar GLOB postderef parses");
319 ok (!eval { $pvbm->$* }, 'PVBM is not a SCALAR ref');
320 ok (!eval { $pvbm->@* }, 'PVBM is not an ARRAY ref');
321 ok (!eval { $pvbm->%* }, 'PVBM is not a HASH ref');
323 # Test undefined hash references as arguments to %{} in boolean context
327 eval { my $foo; $foo->%*; }; ok !$@, '%$undef';
328 eval { my $foo; scalar $foo->%*; }; ok !$@, 'scalar %$undef';
329 eval { my $foo; !$foo->%*; }; ok !$@, '!%$undef';
330 eval { my $foo; if ( $foo->%*) {} }; ok !$@, 'if ( %$undef) {}';
331 eval { my $foo; if (!$foo->%*) {} }; ok !$@, 'if (!%$undef) {}';
332 eval { my $foo; unless ( $foo->%*) {} }; ok !$@, 'unless ( %$undef) {}';
333 eval { my $foo; unless (!$foo->%*) {} }; ok !$@, 'unless (!%$undef) {}';
334 eval { my $foo; 1 if $foo->%*; }; ok !$@, '1 if %$undef';
335 eval { my $foo; 1 if !$foo->%*; }; ok !$@, '1 if !%$undef';
336 eval { my $foo; 1 unless $foo->%*; }; ok !$@, '1 unless %$undef;';
337 eval { my $foo; 1 unless ! $foo->%*; }; ok !$@, '1 unless ! %$undef';
338 eval { my $foo; $foo->%* ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0';
339 eval { my $foo; !$foo->%* ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0';
342 # Postfix key/value slices
343 is join(" ", {1..10}->%{1, 7, 3}), "1 2 7 8 3 4", '->%{';
344 is join(" ", ['a'..'z']->%[1, 7, 3]), "1 b 7 h 3 d", '->%[';
347 is [1..10]->$#*, 9, 'rvalue ->$#*';
350 is "@foo", "1 2 3 4 5 6 7 8 9", 'lvalue ->$#*';
355 %foo = qw( foo oof );
357 no warnings 'deprecated';
359 is "$_->$*", 'foo->42', '->$* interpolation without feature';
361 is "$_->$#*", 'foo->43*', '->$#* interpolation without feature';
363 is "$_->@*", 'foo->@*', '->@* does not interpolate without feature';
364 is "$_->@[0]", 'foo->@[0]', '->@[ does not interpolate without feature';
365 is "$_->@{foo}", "foo->7 8 9", '->@{ does not interpolate without feature';
367 use feature 'postderef_qq';
370 is "$_->$*", "43", '->$* interpolated';
371 is "$_->$#*", "2", '->$#* interpolated';
372 is "$_->@*", "7 8 9", '->@* interpolated';
373 is "$_->@[0,1]", "7 8", '->@[ interpolated';
374 is "$_->@{foo}", "oof", '->@{ interpolated';
375 is "foo$_->$*bar", "foo43bar", '->$* interpolated w/other stuff';
376 is "foo$_->@*bar", "foo7 8 9bar", '->@* interpolated w/other stuff';
377 is "foo$_->@[0,1]bar", "foo7 8bar", '->@[ interpolated w/other stuff';
378 is "foo$_->@{foo}bar", "foooofbar", '->@{ interpolated w/other stuff';
379 is "@{[foo->@*]}", "7 8 9", '->@* inside "@{...}"';
380 is "@{[foo->@[0,1]]}", "7 8", '->@[ inside "@{...}"';
381 is "@{[foo->@{foo}]}", "oof", '->@{ inside "@{...}"';