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'
387 # This used to fail an assertion, but only as a standalone script
388 is runperl(switches => ['-lXMfeature=:all'],
389 prog => 'state sub x {}; undef &x; print defined &x',
390 stderr => 1), "\n", 'undefining state sub';
392 state sub x { is +(caller 0)[3], 'x', 'state sub name in caller' }
396 state sub _cmp { $b cmp $a }
397 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
398 'sort state_sub LIST'
401 state sub handel { "" }
402 print handel, "ok ", curr_test(),
403 " - no 'No comma allowed' after state sub\n";
404 curr_test(curr_test()+1);
407 # -------------------- my -------------------- #
411 isnt \&::foo, \&foo, 'my sub is not stored in the package';
412 is foo, 44, 'calling my sub from same package';
413 is &foo, 44, 'calling my sub from same package (amper)';
415 is foo, 44, 'calling my sub from another package';
416 is &foo, 44, 'calling my sub from another package (amper)';
419 is foo, 43, 'my sub falling out of scope';
420 is &foo, 43, 'my sub falling out of scope (called via amper)';
425 is ma, 43, 'my sub invisible inside itself';
426 is &ma, 43, 'my sub invisible inside itself (called via amper)';
435 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
436 # declaration. Being invisible inside itself, it sees the stub.
438 like $@, qr/^Undefined subroutine &mb called at /,
439 'my sub foo {} after forward declaration';
441 like $@, qr/^Undefined subroutine &mb called at /,
442 'my sub foo {} after forward declaration (amper)';
452 is mb2, 44, 'my sub visible inside itself after decl';
453 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
460 my sub mb3 { # new pad entry
461 # The sub containing this comment is invisible inside itself.
462 # So this one here will assign to the outer pad entry:
467 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
468 # Same test again, but inside an anonymous sub
477 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
484 like $@, qr/^Undefined subroutine &mc called at /,
485 'my sub foo; makes no lex alias for existing sub';
487 like $@, qr/^Undefined subroutine &mc called at /,
488 'my sub foo; makes no lex alias for existing sub (amper)';
493 is prototype eval{\&me}, '$', 'my sub with proto';
494 is prototype "me", undef, 'prototype "..." ignores my subs';
496 my $coderef = eval "my sub foo (\$\x{30cd}) {1}; \\&foo";
497 my $proto = prototype $coderef;
498 ok(utf8::is_utf8($proto), "my sub with UTF8 proto maintains the UTF8ness");
499 is($proto, "\$\x{30cd}", "check the prototypes actually match");
504 is $x, 44, 'my subs override all keywords';
507 is $y, 44, 'my subs from other packages override all keywords';
510 use warnings; no warnings "experimental::lexical_subs";
512 local $SIG{__WARN__} = sub { $w .= shift };
513 eval '#line 87 squidges
518 '"my" subroutine &foo masks earlier declaration in same scope at '
519 . "squidges line 88.\n",
520 'warning for my sub masking earlier declaration';
522 # Test that my subs are cloned inside anonymous subs.
530 $sub1 = mmake_closure 48;
531 $sub2 = mmake_closure 49;
532 is &$sub1, 48, 'my sub in closure (1)';
533 is &$sub2, 49, 'my sub in closure (2)';
534 # Test that they are cloned in named subs.
536 use warnings; no warnings "experimental::lexical_subs";
538 local $SIG{__WARN__} = sub { $w .= shift };
539 eval '#line 65 teetet
546 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
547 my $poom = mfoom(27);
548 my $poom2 = mfoom(678);
549 is $poom->(), 27, 'my subs closing over outer my var (1)';
550 is $poom2->(), 678, 'my subs closing over outer my var (2)';
554 my sub etetetet { $x }
556 is etetetet, 765, 'my sub respects for() localisation';
557 is aoeu, 43, 'unless it is declared outside the for loop';
560 # And we also need to test that multiple my subs can close over each
561 # other’s entries in the parent subs pad, and that cv_clone is not con-
563 sub make_anon_with_my_sub{
568 if (@_) { return eval { \&s1 } }
569 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
570 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
574 # Test my subs inside predeclared my subs
579 my sub s3 { eval '$x' }
582 is s2, 3, 'my sub inside predeclared my sub';
586 my $s = make_anon_with_my_sub;
589 # And make sure the my subs were actually cloned.
590 isnt make_anon_with_my_sub->(0), &$s(0),
591 'my subs in anon subs are cloned';
592 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
595 my sub BEGIN { exit };
596 pass 'my subs are never special blocks';
598 is END('jkqeudth'), jkqeudth,
599 'my sub END {shift} implies @_, not @ARGV';
603 use warnings; no warnings "experimental::lexical_subs";
605 local $SIG{__WARN__} = sub { $w .= shift };
606 eval "#line 56 pygpyf\nsub redef {}";
607 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
608 "sub redefinition warnings from my subs";
613 sub { eval "#line 87 khaki\n\\&x" }
615 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
616 "unavailability warning during compilation of eval in closure";
629 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
630 "unavailability warning during compilation of named sub in anon";
641 my($f,$l) = (__FILE__,__LINE__ - 1);
642 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
643 'unavailability warning during cloning';
645 is $@, "Undefined subroutine &x called at $f line $l.\n",
646 'Vivified sub is correctly named';
653 my $x = 'khaki car keys for the khaki car';
656 is $x, 'khaki car keys for the khaki car',
657 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
666 is ref $_[0], 'ARRAY', 'my sub with proto';
671 is q(), 46, 'my constant called with parens';
676 sub x { x() if $count++ < 10 }
678 is $count, 11, 'my recursive subs';
683 is x, 3, 'my sub defined inside eval';
688 local $SIG{__WARN__} = sub { $w .= shift };
689 eval q{ my sub george () { 2 } };
690 is $w, undef, 'no double free from constant my subs';
693 switches => [ '-Mfeature=lexical_subs,state' ],
694 prog => 'my sub a { foo ref } a()',
698 'referencing a my sub after a syntax error does not crash';
702 my sub foo{ $stuff .= our $AUTOLOAD }
703 *A::AUTOLOAD = \&foo;
706 is $stuff, 'A::bar', 'my sub assigned to *AUTOLOAD can autoload';
709 my sub quire{qr "quires"}
710 package mo { use overload qr => \&quire }
711 ok "quires" =~ bless([], mo::), 'my sub used as overload method';
716 local *mcvgv2 = *mcvgv;
717 eval 'sub mcvgv2 {42}'; # uses the stub already present
718 is foo, 42, 'defining my sub body via package sub declaration';
723 local *mcvgv4 = *mcvgv3;
724 eval 'sub mcvgv4 {42}'; # uses the stub already present
725 undef *mcvgv3; undef *mcvgv4; # leaves the pad with the only reference
727 # We would have crashed by now if it weren’t fixed.
728 pass "pad taking ownership once more of packagified my-sub";
731 local $ENV{PERL5DB} = 'sub DB::DB{}';
734 switches => [ '-d' ],
735 progs => [ split "\n",
736 'use feature qw - lexical_subs state -;
737 no warnings q-experimental::lexical_subs-;
738 sub DB::sub{ print qq|4\n|; goto $DB::sub }
739 my sub foo {print qq|2\n|}
746 'my subs and DB::sub under -d'
749 # This used to fail an assertion, but only as a standalone script
750 is runperl(switches => ['-lXMfeature=:all'],
751 prog => 'my sub x {}; undef &x; print defined &x',
752 stderr => 1), "\n", 'undefining my sub';
754 my sub x { is +(caller 0)[3], 'x', 'my sub name in caller' }
758 my sub _cmp { $b cmp $a }
759 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
764 print handel,"ok ",curr_test()," - no 'No comma allowed' after my sub\n";
765 curr_test(curr_test()+1);
768 # -------------------- Interactions (and misc tests) -------------------- #
773 sub s1 { state sub foo { \&s2 } foo }
775 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
779 sub not_lexical { state sub foo { \&s2 } foo }
780 is not_lexical->(), 3, 'state subs that reference my sub from outside';
783 # Test my subs inside predeclared package subs
784 # This test also checks that CvOUTSIDE pointers are not mangled when the
785 # inner sub’s CvOUTSIDE points to another sub.
796 is not_lexical3, 23, 'my subs inside predeclared package subs';
798 # Test my subs inside predeclared package sub, where the lexical sub is
799 # declared outside the package sub.
800 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
801 # not declared inside the sub that its CvOUTSIDE points to.
812 is not_lexical4, 234,
813 'my sub defined in predeclared pkg sub but declared outside';
818 sub not_lexical6 { sub foo { } }
819 pass 'no crash when cloning a mysub declared inside an undef pack sub';
823 eval 'sub not_lexical7 { my @x }';
830 is ref \$x, 'SCALAR',
831 "redeffing a mysub's outside does not make it use the wrong pad"
837 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
838 prog => 'my sub foo; sub foo { foo } foo',
841 qr/Deep recursion on subroutine "foo"/,
842 'deep recursion warnings for lexical subs do not crash';
845 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
846 prog => 'my sub foo() { 42 } undef &foo',
849 qr/Constant subroutine foo undefined at /,
850 'constant undefinition warnings for lexical subs do not crash';
854 *AutoloadTestSuper::blah = \&foo;
855 sub AutoloadTestSuper::AUTOLOAD {
856 is $AutoloadTestSuper::AUTOLOAD, "AutoloadTestSuper::blah",
857 "Autoloading via inherited lex stub";
859 @AutoloadTest::ISA = AutoloadTestSuper::;