12 # -------------------- Errors with feature disabled -------------------- #
14 eval "#line 8 foo\nmy sub foo";
15 is $@, qq 'Experimental "my" subs not enabled at foo line 8.\n',
16 'my sub unexperimental error';
17 eval "#line 8 foo\nCORE::state sub foo";
18 is $@, qq 'Experimental "state" subs not enabled at foo line 8.\n',
19 'state sub unexperimental error';
20 eval "#line 8 foo\nour sub foo";
21 is $@, qq 'Experimental "our" subs not enabled at foo line 8.\n',
22 'our sub unexperimental error';
24 # -------------------- our -------------------- #
26 no warnings "experimental::lexical_subs";
27 use feature 'lexical_subs';
30 is foo, 42, 'calling our sub from same package';
31 is &foo, 42, 'calling our sub from same package (amper)';
34 is foo, 42, 'calling our sub from another package';
35 is &foo, 42, 'calling our sub from another package (amper)';
38 is foo, 43, 'our sub falling out of scope';
39 is &foo, 43, 'our sub falling out of scope (called via amper)';
46 is a, 43, 'our sub invisible inside itself';
47 is &a, 43, 'our sub invisible inside itself (called via amper)';
57 is b, 42, 'our sub visible inside itself after decl';
58 is &b, 42, 'our sub visible inside itself after decl (amper)';
69 is c, 42, 'our sub foo; makes lex alias for existing sub';
70 is &c, 42, 'our sub foo; makes lex alias for existing sub (amper)';
77 is eval ::d, 'd42', 'our sub foo; applies to subsequent sub foo {}';
81 is prototype "::e", '$', 'our sub with proto';
86 is $x, 42, 'lexical subs (even our) override all keywords';
89 is $y, 42, 'our subs from other packages override all keywords';
91 # Interaction with ‘use constant’
93 our sub const; # symtab now has an undefined CV
94 BEGIN { delete $::{const} } # delete symtab entry; pad entry still exists
95 use constant const => 3; # symtab now has a scalar ref
96 # inlining this used to fail an assertion (parentheses necessary):
97 is(const, 3, 'our sub pointing to "use constant" constant');
99 # our sub and method confusion
103 our sub h { ++$called; 4343 };
104 is((h F),4242, 'our sub symbol translation does not affect meth names');
107 print h F; # follows a different path through yylex to intuit_method
109 is $called, undef, 'our sub symbol translation & meth names after print'
113 =>, 'j', 'name_of_our_sub <newline> => is parsed properly';
114 sub _cmp { $a cmp $b }
115 sub bar::_cmp { $b cmp $a }
120 is join(" ", sort _cmp split //, 'oursub'), 'u u s r o b', 'sort our_sub'
123 # -------------------- state -------------------- #
125 use feature 'state'; # state
128 isnt \&::foo, \&foo, 'state sub is not stored in the package';
129 is eval foo, 44, 'calling state sub from same package';
130 is eval &foo, 44, 'calling state sub from same package (amper)';
132 is eval foo, 44, 'calling state sub from another package';
133 is eval &foo, 44, 'calling state sub from another package (amper)';
136 is foo, 43, 'state sub falling out of scope';
137 is &foo, 43, 'state sub falling out of scope (called via amper)';
142 is sa, 43, 'state sub invisible inside itself';
143 is &sa, 43, 'state sub invisible inside itself (called via amper)';
152 # ‘state sub foo{}’ creates a new pad entry, not reusing the forward
153 # declaration. Being invisible inside itself, it sees the stub.
155 like $@, qr/^Undefined subroutine &sb called at /,
156 'state sub foo {} after forward declaration';
158 like $@, qr/^Undefined subroutine &sb called at /,
159 'state sub foo {} after forward declaration (amper)';
169 is sb2, 44, 'state sub visible inside itself after decl';
170 is &sb2, 44, 'state sub visible inside itself after decl (amper)';
177 state sub sb3 { # new pad entry
178 # The sub containing this comment is invisible inside itself.
179 # So this one here will assign to the outer pad entry:
184 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
185 # Same test again, but inside an anonymous sub
194 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
201 like $@, qr/^Undefined subroutine &sc called at /,
202 'state sub foo; makes no lex alias for existing sub';
204 like $@, qr/^Undefined subroutine &sc called at /,
205 'state sub foo; makes no lex alias for existing sub (amper)';
210 is prototype eval{\&se}, '$', 'state sub with proto';
211 is prototype "se", undef, 'prototype "..." ignores state subs';
214 state sub if() { 44 }
216 is $x, 44, 'state subs override all keywords';
219 is $y, 44, 'state subs from other packages override all keywords';
222 use warnings; no warnings "experimental::lexical_subs";
224 local $SIG{__WARN__} = sub { $w .= shift };
225 eval '#line 87 squidges
230 '"state" subroutine &foo masks earlier declaration in same scope at '
231 . "squidges line 88.\n",
232 'warning for state sub masking earlier declaration';
234 # Since state vars inside anonymous subs are cloned at the same time as the
235 # anonymous subs containing them, the same should happen for state subs.
243 $sub1 = make_closure 48;
244 $sub2 = make_closure 49;
245 is &$sub1, 48, 'state sub in closure (1)';
246 is &$sub2, 49, 'state sub in closure (2)';
247 # But we need to test that state subs actually do persist from one invoca-
248 # tion of a named sub to another (i.e., that they are not my subs).
250 use warnings; no warnings "experimental::lexical_subs";
252 local $SIG{__WARN__} = sub { $w .= shift };
253 eval '#line 65 teetet
256 state sub poom { $x }
260 is $w, "Variable \"\$x\" will not stay shared at teetet line 67.\n",
261 'state subs get "Variable will not stay shared" messages';
263 my $poom2 = foom(678);
264 is eval{$poom->()}, eval {$poom2->()},
265 'state subs close over the first outer my var, like pkg subs';
268 state sub etetetet { $x }
269 is eval{etetetet}, 43, 'state sub ignores for() localisation';
272 # And we also need to test that multiple state subs can close over each
273 # other’s entries in the parent subs pad, and that cv_clone is not con-
275 sub make_anon_with_state_sub{
278 state sub s2 { \&s1 }
280 if (@_) { return \&s1 }
281 is s1,\&s2, 'state sub in anon closure closing over sibling state sub';
282 is s2,\&s1, 'state sub in anon closure closing over sibling state sub';
286 my $s = make_anon_with_state_sub;
289 # And make sure the state subs were actually cloned.
290 isnt make_anon_with_state_sub->(0), &$s(0),
291 'state subs in anon subs are cloned';
292 is &$s(0), &$s(0), 'but only when the anon sub is cloned';
295 state sub BEGIN { exit };
296 pass 'state subs are never special blocks';
297 state sub END { shift }
298 is eval{END('jkqeudth')}, jkqeudth,
299 'state sub END {shift} implies @_, not @ARGV';
300 state sub CORE { scalar reverse shift }
301 is CORE::uc("hello"), "HELLO",
302 'lexical CORE does not interfere with CORE::...';
306 use warnings; no warnings "experimental::lexical_subs";
308 local $SIG{__WARN__} = sub { $w .= shift };
309 eval "#line 56 pygpyf\nsub redef {}";
310 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
311 "sub redefinition warnings from state subs";
315 is ref $_[0], 'ARRAY', 'state sub with proto';
319 state sub q () { 45 }
320 is q(), 45, 'state constant called with parens';
325 is x, 3, 'state sub defined inside eval';
329 if (@_) { # outer call
332 'state sub run-time redefinition applies to all recursion levels';
335 eval 'sub foo { 42 }';
341 switches => [ '-Mfeature=lexical_subs,state' ],
342 prog => 'state sub a { foo ref } a()',
346 'referencing a state sub after a syntax error does not crash';
350 state sub foo{ $stuff .= our $AUTOLOAD }
351 *A::AUTOLOAD = \&foo;
354 is $stuff, 'A::bar', 'state sub assigned to *AUTOLOAD can autoload';
357 state sub quire{qr "quires"}
358 package o { use overload qr => \&quire }
359 ok "quires" =~ bless([], o::), 'state sub used as overload method';
364 local *cvgv2 = *cvgv;
365 eval 'sub cvgv2 {42}'; # uses the stub already present
366 is foo, 42, 'defining state sub body via package sub declaration';
369 local $ENV{PERL5DB} = 'sub DB::DB{}';
372 switches => [ '-d' ],
373 progs => [ split "\n",
374 'use feature qw - lexical_subs state -;
375 no warnings q-experimental::lexical_subs-;
376 sub DB::sub{ print qq|4\n|; goto $DB::sub }
377 state sub foo {print qq|2\n|}
384 'state subs and DB::sub under -d'
388 switches => [ '-d' ],
389 progs => [ split "\n",
390 'use feature qw - lexical_subs state -;
391 no warnings q-experimental::lexical_subs-;
392 sub DB::goto{ print qq|4\n|; $_ = $DB::sub }
393 state sub foo {print qq|2\n|}
395 sub { goto &foo }->();
396 print $_ == \&foo ? qq|ok\n| : qq|$_\n|;
402 'state subs and DB::goto under -d'
405 # This used to fail an assertion, but only as a standalone script
406 is runperl(switches => ['-lXMfeature=:all'],
407 prog => 'state sub x {}; undef &x; print defined &x',
408 stderr => 1), "\n", 'undefining state sub';
410 state sub x { is +(caller 0)[3], 'x', 'state sub name in caller' }
414 state sub _cmp { $b cmp $a }
415 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
416 'sort state_sub LIST'
419 state sub handel { "" }
420 print handel, "ok ", curr_test(),
421 " - no 'No comma allowed' after state sub\n";
422 curr_test(curr_test()+1);
425 # -------------------- my -------------------- #
429 isnt \&::foo, \&foo, 'my sub is not stored in the package';
430 is foo, 44, 'calling my sub from same package';
431 is &foo, 44, 'calling my sub from same package (amper)';
433 is foo, 44, 'calling my sub from another package';
434 is &foo, 44, 'calling my sub from another package (amper)';
437 is foo, 43, 'my sub falling out of scope';
438 is &foo, 43, 'my sub falling out of scope (called via amper)';
443 is ma, 43, 'my sub invisible inside itself';
444 is &ma, 43, 'my sub invisible inside itself (called via amper)';
453 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
454 # declaration. Being invisible inside itself, it sees the stub.
456 like $@, qr/^Undefined subroutine &mb called at /,
457 'my sub foo {} after forward declaration';
459 like $@, qr/^Undefined subroutine &mb called at /,
460 'my sub foo {} after forward declaration (amper)';
470 is mb2, 44, 'my sub visible inside itself after decl';
471 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
478 my sub mb3 { # new pad entry
479 # The sub containing this comment is invisible inside itself.
480 # So this one here will assign to the outer pad entry:
485 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
486 # Same test again, but inside an anonymous sub
495 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
502 like $@, qr/^Undefined subroutine &mc called at /,
503 'my sub foo; makes no lex alias for existing sub';
505 like $@, qr/^Undefined subroutine &mc called at /,
506 'my sub foo; makes no lex alias for existing sub (amper)';
511 is prototype eval{\&me}, '$', 'my sub with proto';
512 is prototype "me", undef, 'prototype "..." ignores my subs';
514 my $coderef = eval "my sub foo (\$\x{30cd}) {1}; \\&foo";
515 my $proto = prototype $coderef;
516 ok(utf8::is_utf8($proto), "my sub with UTF8 proto maintains the UTF8ness");
517 is($proto, "\$\x{30cd}", "check the prototypes actually match");
522 is $x, 44, 'my subs override all keywords';
525 is $y, 44, 'my subs from other packages override all keywords';
528 use warnings; no warnings "experimental::lexical_subs";
530 local $SIG{__WARN__} = sub { $w .= shift };
531 eval '#line 87 squidges
536 '"my" subroutine &foo masks earlier declaration in same scope at '
537 . "squidges line 88.\n",
538 'warning for my sub masking earlier declaration';
540 # Test that my subs are cloned inside anonymous subs.
548 $sub1 = mmake_closure 48;
549 $sub2 = mmake_closure 49;
550 is &$sub1, 48, 'my sub in closure (1)';
551 is &$sub2, 49, 'my sub in closure (2)';
552 # Test that they are cloned in named subs.
554 use warnings; no warnings "experimental::lexical_subs";
556 local $SIG{__WARN__} = sub { $w .= shift };
557 eval '#line 65 teetet
564 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
565 my $poom = mfoom(27);
566 my $poom2 = mfoom(678);
567 is $poom->(), 27, 'my subs closing over outer my var (1)';
568 is $poom2->(), 678, 'my subs closing over outer my var (2)';
572 my sub etetetet { $x }
574 is etetetet, 765, 'my sub respects for() localisation';
575 is aoeu, 43, 'unless it is declared outside the for loop';
578 # And we also need to test that multiple my subs can close over each
579 # other’s entries in the parent subs pad, and that cv_clone is not con-
581 sub make_anon_with_my_sub{
586 if (@_) { return eval { \&s1 } }
587 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
588 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
592 # Test my subs inside predeclared my subs
597 my sub s3 { eval '$x' }
600 is s2, 3, 'my sub inside predeclared my sub';
604 my $s = make_anon_with_my_sub;
607 # And make sure the my subs were actually cloned.
608 isnt make_anon_with_my_sub->(0), &$s(0),
609 'my subs in anon subs are cloned';
610 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
613 my sub BEGIN { exit };
614 pass 'my subs are never special blocks';
616 is END('jkqeudth'), jkqeudth,
617 'my sub END {shift} implies @_, not @ARGV';
621 use warnings; no warnings "experimental::lexical_subs";
623 local $SIG{__WARN__} = sub { $w .= shift };
624 eval "#line 56 pygpyf\nsub redef {}";
625 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
626 "sub redefinition warnings from my subs";
631 sub { eval "#line 87 khaki\n\\&x" }
633 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
634 "unavailability warning during compilation of eval in closure";
647 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
648 "unavailability warning during compilation of named sub in anon";
659 my($f,$l) = (__FILE__,__LINE__ - 1);
660 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
661 'unavailability warning during cloning';
663 is $@, "Undefined subroutine &x called at $f line $l.\n",
664 'Vivified sub is correctly named';
671 my $x = 'khaki car keys for the khaki car';
674 is $x, 'khaki car keys for the khaki car',
675 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
684 is ref $_[0], 'ARRAY', 'my sub with proto';
689 is q(), 46, 'my constant called with parens';
694 sub x { x() if $count++ < 10 }
696 is $count, 11, 'my recursive subs';
701 is x, 3, 'my sub defined inside eval';
706 local $SIG{__WARN__} = sub { $w .= shift };
707 eval q{ my sub george () { 2 } };
708 is $w, undef, 'no double free from constant my subs';
711 switches => [ '-Mfeature=lexical_subs,state' ],
712 prog => 'my sub a { foo ref } a()',
716 'referencing a my sub after a syntax error does not crash';
720 my sub foo{ $stuff .= our $AUTOLOAD }
721 *A::AUTOLOAD = \&foo;
724 is $stuff, 'A::bar', 'my sub assigned to *AUTOLOAD can autoload';
727 my sub quire{qr "quires"}
728 package mo { use overload qr => \&quire }
729 ok "quires" =~ bless([], mo::), 'my sub used as overload method';
734 local *mcvgv2 = *mcvgv;
735 eval 'sub mcvgv2 {42}'; # uses the stub already present
736 is foo, 42, 'defining my sub body via package sub declaration';
741 local *mcvgv4 = *mcvgv3;
742 eval 'sub mcvgv4 {42}'; # uses the stub already present
743 undef *mcvgv3; undef *mcvgv4; # leaves the pad with the only reference
745 # We would have crashed by now if it weren’t fixed.
746 pass "pad taking ownership once more of packagified my-sub";
749 local $ENV{PERL5DB} = 'sub DB::DB{}';
752 switches => [ '-d' ],
753 progs => [ split "\n",
754 'use feature qw - lexical_subs state -;
755 no warnings q-experimental::lexical_subs-;
756 sub DB::sub{ print qq|4\n|; goto $DB::sub }
757 my sub foo {print qq|2\n|}
764 'my subs and DB::sub under -d'
767 # This used to fail an assertion, but only as a standalone script
768 is runperl(switches => ['-lXMfeature=:all'],
769 prog => 'my sub x {}; undef &x; print defined &x',
770 stderr => 1), "\n", 'undefining my sub';
772 my sub x { is +(caller 0)[3], 'x', 'my sub name in caller' }
776 my sub _cmp { $b cmp $a }
777 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
782 print handel,"ok ",curr_test()," - no 'No comma allowed' after my sub\n";
783 curr_test(curr_test()+1);
786 # -------------------- Interactions (and misc tests) -------------------- #
791 sub s1 { state sub foo { \&s2 } foo }
793 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
797 sub not_lexical { state sub foo { \&s2 } foo }
798 is not_lexical->(), 3, 'state subs that reference my sub from outside';
801 # Test my subs inside predeclared package subs
802 # This test also checks that CvOUTSIDE pointers are not mangled when the
803 # inner sub’s CvOUTSIDE points to another sub.
814 is not_lexical3, 23, 'my subs inside predeclared package subs';
816 # Test my subs inside predeclared package sub, where the lexical sub is
817 # declared outside the package sub.
818 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
819 # not declared inside the sub that its CvOUTSIDE points to.
830 is not_lexical4, 234,
831 'my sub defined in predeclared pkg sub but declared outside';
836 sub not_lexical6 { sub foo { } }
837 pass 'no crash when cloning a mysub declared inside an undef pack sub';
841 eval 'sub not_lexical7 { my @x }';
848 is ref \$x, 'SCALAR',
849 "redeffing a mysub's outside does not make it use the wrong pad"
855 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
856 prog => 'my sub foo; sub foo { foo } foo',
859 qr/Deep recursion on subroutine "foo"/,
860 'deep recursion warnings for lexical subs do not crash';
863 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
864 prog => 'my sub foo() { 42 } undef &foo',
867 qr/Constant subroutine foo undefined at /,
868 'constant undefinition warnings for lexical subs do not crash';
872 *AutoloadTestSuper::blah = \&foo;
873 sub AutoloadTestSuper::AUTOLOAD {
874 is $AutoloadTestSuper::AUTOLOAD, "AutoloadTestSuper::blah",
875 "Autoloading via inherited lex stub";
877 @AutoloadTest::ISA = AutoloadTestSuper::;