10 no warnings 'deprecated';
13 # -------------------- our -------------------- #
17 is foo, 42, 'calling our sub from same package';
18 is &foo, 42, 'calling our sub from same package (amper)';
19 is do foo(), 42, 'calling our sub from same package (do)';
22 is foo, 42, 'calling our sub from another package';
23 is &foo, 42, 'calling our sub from another package (amper)';
24 is do foo(), 42, 'calling our sub from another package (do)';
27 is foo, 43, 'our sub falling out of scope';
28 is &foo, 43, 'our sub falling out of scope (called via amper)';
29 is do foo(), 43, 'our sub falling out of scope (called via amper)';
36 is a, 43, 'our sub invisible inside itself';
37 is &a, 43, 'our sub invisible inside itself (called via amper)';
38 is do a(), 43, 'our sub invisible inside itself (called via do)';
48 is b, 42, 'our sub visible inside itself after decl';
49 is &b, 42, 'our sub visible inside itself after decl (amper)';
50 is do b(), 42, 'our sub visible inside itself after decl (do)';
61 is c, 42, 'our sub foo; makes lex alias for existing sub';
62 is &c, 42, 'our sub foo; makes lex alias for existing sub (amper)';
63 is do c(), 42, 'our sub foo; makes lex alias for existing sub (do)';
70 is eval ::d, 'd42', 'our sub foo; applies to subsequent sub foo {}';
74 is prototype "::e", '$', 'our sub with proto';
79 is $x, 42, 'lexical subs (even our) override all keywords';
82 is $y, 42, 'our subs from other packages override all keywords';
85 # -------------------- state -------------------- #
90 isnt \&::foo, \&foo, 'state sub is not stored in the package';
91 is eval foo, 44, 'calling state sub from same package';
92 is eval &foo, 44, 'calling state sub from same package (amper)';
93 is eval do foo(), 44, 'calling state sub from same package (do)';
95 is eval foo, 44, 'calling state sub from another package';
96 is eval &foo, 44, 'calling state sub from another package (amper)';
97 is eval do foo(), 44, 'calling state sub from another package (do)';
100 is foo, 43, 'state sub falling out of scope';
101 is &foo, 43, 'state sub falling out of scope (called via amper)';
102 is do foo(), 43, 'state sub falling out of scope (called via amper)';
107 is sa, 43, 'state sub invisible inside itself';
108 is &sa, 43, 'state sub invisible inside itself (called via amper)';
109 is do sa(), 43, 'state sub invisible inside itself (called via do)';
118 # ‘state sub foo{}’ creates a new pad entry, not reusing the forward
119 # declaration. Being invisible inside itself, it sees the stub.
121 like $@, qr/^Undefined subroutine &sb called at /,
122 'state sub foo {} after forward declaration';
124 like $@, qr/^Undefined subroutine &sb called at /,
125 'state sub foo {} after forward declaration (amper)';
127 like $@, qr/^Undefined subroutine &sb called at /,
128 'state sub foo {} after forward declaration (do)';
138 is sb2, 44, 'state sub visible inside itself after decl';
139 is &sb2, 44, 'state sub visible inside itself after decl (amper)';
140 is do sb2(), 44, 'state sub visible inside itself after decl (do)';
147 state sub sb3 { # new pad entry
148 # The sub containing this comment is invisible inside itself.
149 # So this one here will assign to the outer pad entry:
154 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
155 # Same test again, but inside an anonymous sub
164 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
171 like $@, qr/^Undefined subroutine &sc called at /,
172 'state sub foo; makes no lex alias for existing sub';
174 like $@, qr/^Undefined subroutine &sc called at /,
175 'state sub foo; makes no lex alias for existing sub (amper)';
177 like $@, qr/^Undefined subroutine &sc called at /,
178 'state sub foo; makes no lex alias for existing sub (do)';
183 is prototype eval{\&se}, '$', 'state sub with proto';
184 is prototype "se", undef, 'prototype "..." ignores state subs';
187 state sub if() { 44 }
189 is $x, 44, 'state subs override all keywords';
192 is $y, 44, 'state subs from other packages override all keywords';
197 local $SIG{__WARN__} = sub { $w .= shift };
198 eval '#line 87 squidges
203 '"state" subroutine &foo masks earlier declaration in same scope at '
204 . "squidges line 88.\n",
205 'warning for state sub masking earlier declaration';
207 # Since state vars inside anonymous subs are cloned at the same time as the
208 # anonymous subs containing them, the same should happen for state subs.
216 $sub1 = make_closure 48;
217 $sub2 = make_closure 49;
218 is &$sub1, 48, 'state sub in closure (1)';
219 is &$sub2, 49, 'state sub in closure (2)';
220 # But we need to test that state subs actually do persist from one invoca-
221 # tion of a named sub to another (i.e., that they are not my subs).
225 local $SIG{__WARN__} = sub { $w .= shift };
226 eval '#line 65 teetet
229 state sub poom { $x }
233 is $w, "Variable \"\$x\" will not stay shared at teetet line 67.\n",
234 'state subs get "Variable will not stay shared" messages';
236 my $poom2 = foom(678);
237 is eval{$poom->()}, eval {$poom2->()},
238 'state subs close over the first outer my var, like pkg subs';
241 state sub etetetet { $x }
242 is eval{etetetet}, 43, 'state sub ignores for() localisation';
245 # And we also need to test that multiple state subs can close over each
246 # other’s entries in the parent subs pad, and that cv_clone is not con-
248 sub make_anon_with_state_sub{
251 state sub s2 { \&s1 }
253 if (@_) { return \&s1 }
254 is s1,\&s2, 'state sub in anon closure closing over sibling state sub';
255 is s2,\&s1, 'state sub in anon closure closing over sibling state sub';
259 my $s = make_anon_with_state_sub;
262 # And make sure the state subs were actually cloned.
263 isnt make_anon_with_state_sub->(0), &$s(0),
264 'state subs in anon subs are cloned';
265 is &$s(0), &$s(0), 'but only when the anon sub is cloned';
268 state sub BEGIN { exit };
269 pass 'state subs are never special blocks';
270 state sub END { shift }
271 is eval{END('jkqeudth')}, jkqeudth,
272 'state sub END {shift} implies @_, not @ARGV';
278 local $SIG{__WARN__} = sub { $w .= shift };
279 eval "#line 56 pygpyf\nsub redef {}";
280 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
281 "sub redefinition warnings from state subs";
285 is ref $_[0], 'ARRAY', 'state sub with proto';
292 is x, 3, 'state sub defined inside eval';
295 # -------------------- my -------------------- #
299 isnt \&::foo, \&foo, 'my sub is not stored in the package';
300 is foo, 44, 'calling my sub from same package';
301 is &foo, 44, 'calling my sub from same package (amper)';
302 is do foo(), 44, 'calling my sub from same package (do)';
304 is foo, 44, 'calling my sub from another package';
305 is &foo, 44, 'calling my sub from another package (amper)';
306 is do foo(), 44, 'calling my sub from another package (do)';
309 is foo, 43, 'my sub falling out of scope';
310 is &foo, 43, 'my sub falling out of scope (called via amper)';
311 is do foo(), 43, 'my sub falling out of scope (called via amper)';
316 is ma, 43, 'my sub invisible inside itself';
317 is &ma, 43, 'my sub invisible inside itself (called via amper)';
318 is do ma(), 43, 'my sub invisible inside itself (called via do)';
327 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
328 # declaration. Being invisible inside itself, it sees the stub.
330 like $@, qr/^Undefined subroutine &mb called at /,
331 'my sub foo {} after forward declaration';
333 like $@, qr/^Undefined subroutine &mb called at /,
334 'my sub foo {} after forward declaration (amper)';
336 like $@, qr/^Undefined subroutine &mb called at /,
337 'my sub foo {} after forward declaration (do)';
347 is mb2, 44, 'my sub visible inside itself after decl';
348 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
349 is do mb2(), 44, 'my sub visible inside itself after decl (do)';
356 my sub mb3 { # new pad entry
357 # The sub containing this comment is invisible inside itself.
358 # So this one here will assign to the outer pad entry:
363 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
364 # Same test again, but inside an anonymous sub
373 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
380 like $@, qr/^Undefined subroutine &mc called at /,
381 'my sub foo; makes no lex alias for existing sub';
383 like $@, qr/^Undefined subroutine &mc called at /,
384 'my sub foo; makes no lex alias for existing sub (amper)';
386 like $@, qr/^Undefined subroutine &mc called at /,
387 'my sub foo; makes no lex alias for existing sub (do)';
392 is prototype eval{\&me}, '$', 'my sub with proto';
393 is prototype "me", undef, 'prototype "..." ignores my subs';
398 is $x, 44, 'my subs override all keywords';
401 is $y, 44, 'my subs from other packages override all keywords';
406 local $SIG{__WARN__} = sub { $w .= shift };
407 eval '#line 87 squidges
412 '"my" subroutine &foo masks earlier declaration in same scope at '
413 . "squidges line 88.\n",
414 'warning for my sub masking earlier declaration';
416 # Test that my subs are cloned inside anonymous subs.
424 $sub1 = mmake_closure 48;
425 $sub2 = mmake_closure 49;
426 is &$sub1, 48, 'my sub in closure (1)';
427 is &$sub2, 49, 'my sub in closure (2)';
428 # Test that they are cloned in named subs.
432 local $SIG{__WARN__} = sub { $w .= shift };
433 eval '#line 65 teetet
440 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
441 my $poom = mfoom(27);
442 my $poom2 = mfoom(678);
443 is $poom->(), 27, 'my subs closing over outer my var (1)';
444 is $poom2->(), 678, 'my subs closing over outer my var (2)';
448 my sub etetetet { $x }
450 is etetetet, 765, 'my sub respects for() localisation';
451 is aoeu, 43, 'unless it is declared outside the for loop';
454 # And we also need to test that multiple my subs can close over each
455 # other’s entries in the parent subs pad, and that cv_clone is not con-
457 sub make_anon_with_my_sub{
462 if (@_) { return eval { \&s1 } }
463 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
464 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
468 # Test my subs inside predeclared my subs
473 my sub s3 { eval '$x' }
476 is s2, 3, 'my sub inside predeclared my sub';
480 my $s = make_anon_with_my_sub;
483 # And make sure the my subs were actually cloned.
484 isnt make_anon_with_my_sub->(0), &$s(0),
485 'my subs in anon subs are cloned';
486 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
489 my sub BEGIN { exit };
490 pass 'my subs are never special blocks';
492 is END('jkqeudth'), jkqeudth,
493 'my sub END {shift} implies @_, not @ARGV';
499 local $SIG{__WARN__} = sub { $w .= shift };
500 eval "#line 56 pygpyf\nsub redef {}";
501 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
502 "sub redefinition warnings from my subs";
507 sub { eval "#line 87 khaki\n\\&x" }
509 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
510 "unavailability warning during compilation of eval in closure";
523 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
524 "unavailability warning during compilation of named sub in anon";
535 my($f,$l) = (__FILE__,__LINE__ - 1);
536 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
537 'unavailability warning during cloning';
539 is $@, "Undefined subroutine &x called at $f line $l.\n",
540 'Vivified sub is correctly named';
547 my $x = 'khaki car keys for the khaki car';
550 is $x, 'khaki car keys for the khaki car',
551 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
560 is ref $_[0], 'ARRAY', 'my sub with proto';
567 sub x { x() if $count++ < 10 }
569 is $count, 11, 'my recursive subs';
572 # -------------------- Interactions (and misc tests) -------------------- #
577 sub s1 { state sub foo { \&s2 } foo }
579 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
583 sub not_lexical { state sub foo { \&s2 } foo }
584 is not_lexical->(), 3, 'state subs that reference my sub from outside';
587 # Test my subs inside predeclared package subs
588 # This test also checks that CvOUTSIDE pointers are not mangled when the
589 # inner sub’s CvOUTSIDE points to another sub.
600 is not_lexical3, 23, 'my subs inside predeclared package subs';
602 # Test my subs inside predeclared package sub, where the lexical sub is
603 # declared outside the package sub.
604 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
605 # not declared inside the sub that its CvOUTSIDE points to.
616 is not_lexical4, 234,
617 'my sub defined in predeclared pkg sub but declared outside';
622 sub not_lexical6 { sub foo { } }
623 pass 'no crash when cloning a mysub declared inside an undef pack sub';
627 eval 'sub not_lexical7 { my @x }';
634 is ref \$x, 'SCALAR',
635 "redeffing a mysub's outside does not make it use the wrong pad"