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';
294 # Check that nested state subs close over variables properly
307 }->(), 42, 'state sub with body defined in doubly-nested state subs';
319 }->(), 42, 'nested state subs declared in same scope';
321 local $SIG{__WARN__} = sub { $w .= shift };
322 use warnings 'closure';
330 like $w, qr/Variable \"\$x\" is not available at /,
331 "unavailability warning when state closure is defined in anon sub";
334 state sub BEGIN { exit };
335 pass 'state subs are never special blocks';
336 state sub END { shift }
337 is eval{END('jkqeudth')}, jkqeudth,
338 'state sub END {shift} implies @_, not @ARGV';
339 state sub CORE { scalar reverse shift }
340 is CORE::uc("hello"), "HELLO",
341 'lexical CORE does not interfere with CORE::...';
345 use warnings; no warnings "experimental::lexical_subs";
347 local $SIG{__WARN__} = sub { $w .= shift };
348 eval "#line 56 pygpyf\nsub redef {}";
349 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
350 "sub redefinition warnings from state subs";
354 is ref $_[0], 'ARRAY', 'state sub with proto';
358 state sub q () { 45 }
359 is q(), 45, 'state constant called with parens';
364 is x, 3, 'state sub defined inside eval';
368 if (@_) { # outer call
371 'state sub run-time redefinition applies to all recursion levels';
374 eval 'sub foo { 42 }';
380 switches => [ '-Mfeature=lexical_subs,state' ],
381 prog => 'state sub a { foo ref } a()',
385 'referencing a state sub after a syntax error does not crash';
389 state sub foo{ $stuff .= our $AUTOLOAD }
390 *A::AUTOLOAD = \&foo;
393 is $stuff, 'A::bar', 'state sub assigned to *AUTOLOAD can autoload';
396 state sub quire{qr "quires"}
397 package o { use overload qr => \&quire }
398 ok "quires" =~ bless([], o::), 'state sub used as overload method';
403 local *cvgv2 = *cvgv;
404 eval 'sub cvgv2 {42}'; # uses the stub already present
405 is foo, 42, 'defining state sub body via package sub declaration';
408 local $ENV{PERL5DB} = 'sub DB::DB{}';
411 switches => [ '-d' ],
412 progs => [ split "\n",
413 'use feature qw - lexical_subs state -;
414 no warnings q-experimental::lexical_subs-;
416 print qq|4\n| unless $DB::sub =~ DESTROY;
419 state sub foo {print qq|2\n|}
426 'state subs and DB::sub under -d'
430 switches => [ '-d' ],
431 progs => [ split "\n",
432 'use feature qw - lexical_subs state -;
433 no warnings q-experimental::lexical_subs-;
434 sub DB::goto{ print qq|4\n|; $_ = $DB::sub }
435 state sub foo {print qq|2\n|}
437 sub { goto &foo }->();
438 print $_ == \&foo ? qq|ok\n| : qq|$_\n|;
444 'state subs and DB::goto under -d'
447 # This used to fail an assertion, but only as a standalone script
448 is runperl(switches => ['-lXMfeature=:all'],
449 prog => 'state sub x {}; undef &x; print defined &x',
450 stderr => 1), "\n", 'undefining state sub';
452 state sub x { is +(caller 0)[3], 'x', 'state sub name in caller' }
456 state sub _cmp { $b cmp $a }
457 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
458 'sort state_sub LIST'
461 state sub handel { "" }
462 print handel, "ok ", curr_test(),
463 " - no 'No comma allowed' after state sub\n";
464 curr_test(curr_test()+1);
470 like $@, qr/^Undefined subroutine &φου called at /,
471 'state sub with utf8 name';
473 # This used to crash, but only as a standalone script
474 is runperl(switches => ['-lXMfeature=:all'],
475 prog => '$::x = global=>;
479 state sub x { print eval q|$x| }
483 stderr => 1), "42\n",
484 'closure behaviour of state sub in predeclared package sub';
486 # -------------------- my -------------------- #
490 isnt \&::foo, \&foo, 'my sub is not stored in the package';
491 is foo, 44, 'calling my sub from same package';
492 is &foo, 44, 'calling my sub from same package (amper)';
494 is foo, 44, 'calling my sub from another package';
495 is &foo, 44, 'calling my sub from another package (amper)';
498 is foo, 43, 'my sub falling out of scope';
499 is &foo, 43, 'my sub falling out of scope (called via amper)';
504 is ma, 43, 'my sub invisible inside itself';
505 is &ma, 43, 'my sub invisible inside itself (called via amper)';
514 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
515 # declaration. Being invisible inside itself, it sees the stub.
517 like $@, qr/^Undefined subroutine &mb called at /,
518 'my sub foo {} after forward declaration';
520 like $@, qr/^Undefined subroutine &mb called at /,
521 'my sub foo {} after forward declaration (amper)';
531 is mb2, 44, 'my sub visible inside itself after decl';
532 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
539 my sub mb3 { # new pad entry
540 # The sub containing this comment is invisible inside itself.
541 # So this one here will assign to the outer pad entry:
546 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
547 # Same test again, but inside an anonymous sub
556 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
563 like $@, qr/^Undefined subroutine &mc called at /,
564 'my sub foo; makes no lex alias for existing sub';
566 like $@, qr/^Undefined subroutine &mc called at /,
567 'my sub foo; makes no lex alias for existing sub (amper)';
572 is prototype eval{\&me}, '$', 'my sub with proto';
573 is prototype "me", undef, 'prototype "..." ignores my subs';
575 my $coderef = eval "my sub foo (\$\x{30cd}) {1}; \\&foo";
576 my $proto = prototype $coderef;
577 ok(utf8::is_utf8($proto), "my sub with UTF8 proto maintains the UTF8ness");
578 is($proto, "\$\x{30cd}", "check the prototypes actually match");
583 is $x, 44, 'my subs override all keywords';
586 is $y, 44, 'my subs from other packages override all keywords';
589 use warnings; no warnings "experimental::lexical_subs";
591 local $SIG{__WARN__} = sub { $w .= shift };
592 eval '#line 87 squidges
597 '"my" subroutine &foo masks earlier declaration in same scope at '
598 . "squidges line 88.\n",
599 'warning for my sub masking earlier declaration';
601 # Test that my subs are cloned inside anonymous subs.
609 $sub1 = mmake_closure 48;
610 $sub2 = mmake_closure 49;
611 is &$sub1, 48, 'my sub in closure (1)';
612 is &$sub2, 49, 'my sub in closure (2)';
613 # Test that they are cloned in named subs.
615 use warnings; no warnings "experimental::lexical_subs";
617 local $SIG{__WARN__} = sub { $w .= shift };
618 eval '#line 65 teetet
625 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
626 my $poom = mfoom(27);
627 my $poom2 = mfoom(678);
628 is $poom->(), 27, 'my subs closing over outer my var (1)';
629 is $poom2->(), 678, 'my subs closing over outer my var (2)';
633 my sub etetetet { $x }
635 is etetetet, 765, 'my sub respects for() localisation';
636 is aoeu, 43, 'unless it is declared outside the for loop';
639 # And we also need to test that multiple my subs can close over each
640 # other’s entries in the parent subs pad, and that cv_clone is not con-
642 sub make_anon_with_my_sub{
647 if (@_) { return eval { \&s1 } }
648 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
649 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
653 # Test my subs inside predeclared my subs
658 my sub s3 { eval '$x' }
661 is s2, 3, 'my sub inside predeclared my sub';
665 my $s = make_anon_with_my_sub;
668 # And make sure the my subs were actually cloned.
669 isnt make_anon_with_my_sub->(0), &$s(0),
670 'my subs in anon subs are cloned';
671 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
674 my sub BEGIN { exit };
675 pass 'my subs are never special blocks';
677 is END('jkqeudth'), jkqeudth,
678 'my sub END {shift} implies @_, not @ARGV';
682 use warnings; no warnings "experimental::lexical_subs";
684 local $SIG{__WARN__} = sub { $w .= shift };
685 eval "#line 56 pygpyf\nsub redef {}";
686 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
687 "sub redefinition warnings from my subs";
692 sub { eval "#line 87 khaki\n\\&x" }
694 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
695 "unavailability warning during compilation of eval in closure";
708 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
709 "unavailability warning during compilation of named sub in anon";
720 my($f,$l) = (__FILE__,__LINE__ - 1);
721 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
722 'unavailability warning during cloning';
724 is $@, "Undefined subroutine &x called at $f line $l.\n",
725 'Vivified sub is correctly named';
732 my $x = 'khaki car keys for the khaki car';
735 is $x, 'khaki car keys for the khaki car',
736 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
745 is ref $_[0], 'ARRAY', 'my sub with proto';
750 is q(), 46, 'my constant called with parens';
755 sub x { x() if $count++ < 10 }
757 is $count, 11, 'my recursive subs';
762 is x, 3, 'my sub defined inside eval';
767 local $SIG{__WARN__} = sub { $w .= shift };
768 eval q{ my sub george () { 2 } };
769 is $w, undef, 'no double free from constant my subs';
772 switches => [ '-Mfeature=lexical_subs,state' ],
773 prog => 'my sub a { foo ref } a()',
777 'referencing a my sub after a syntax error does not crash';
781 my sub foo{ $stuff .= our $AUTOLOAD }
782 *A::AUTOLOAD = \&foo;
785 is $stuff, 'A::bar', 'my sub assigned to *AUTOLOAD can autoload';
788 my sub quire{qr "quires"}
789 package mo { use overload qr => \&quire }
790 ok "quires" =~ bless([], mo::), 'my sub used as overload method';
795 local *mcvgv2 = *mcvgv;
796 eval 'sub mcvgv2 {42}'; # uses the stub already present
797 is foo, 42, 'defining my sub body via package sub declaration';
802 local *mcvgv4 = *mcvgv3;
803 eval 'sub mcvgv4 {42}'; # uses the stub already present
804 undef *mcvgv3; undef *mcvgv4; # leaves the pad with the only reference
806 # We would have crashed by now if it weren’t fixed.
807 pass "pad taking ownership once more of packagified my-sub";
810 local $ENV{PERL5DB} = 'sub DB::DB{}';
813 switches => [ '-d' ],
814 progs => [ split "\n",
815 'use feature qw - lexical_subs state -;
816 no warnings q-experimental::lexical_subs-;
818 print qq|4\n| unless $DB::sub =~ DESTROY;
821 my sub foo {print qq|2\n|}
828 'my subs and DB::sub under -d'
831 # This used to fail an assertion, but only as a standalone script
832 is runperl(switches => ['-lXMfeature=:all'],
833 prog => 'my sub x {}; undef &x; print defined &x',
834 stderr => 1), "\n", 'undefining my sub';
836 my sub x { is +(caller 0)[3], 'x', 'my sub name in caller' }
840 my sub _cmp { $b cmp $a }
841 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
846 print handel,"ok ",curr_test()," - no 'No comma allowed' after my sub\n";
847 curr_test(curr_test()+1);
851 my sub y :prototype() {$x};
852 is y, 43, 'my sub that looks like constant closure';
858 like $@, qr/^Undefined subroutine &φου called at /,
859 'my sub with utf8 name';
863 local $SIG{__WARN__} = sub { $w = shift };
864 use warnings 'closure';
865 eval 'sub stayshared { my sub x; sub notstayshared { x } } 1' or die;
866 like $w, qr/^Subroutine "&x" will not stay shared at /,
867 'Subroutine will not stay shared';
870 # -------------------- Interactions (and misc tests) -------------------- #
875 sub s1 { state sub foo { \&s2 } foo }
877 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
881 sub not_lexical { state sub foo { \&s2 } foo }
882 is not_lexical->(), 3, 'state subs that reference my sub from outside';
885 # Test my subs inside predeclared package subs
886 # This test also checks that CvOUTSIDE pointers are not mangled when the
887 # inner sub’s CvOUTSIDE points to another sub.
898 is not_lexical3, 23, 'my subs inside predeclared package subs';
900 # Test my subs inside predeclared package sub, where the lexical sub is
901 # declared outside the package sub.
902 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
903 # not declared inside the sub that its CvOUTSIDE points to.
914 is not_lexical4, 234,
915 'my sub defined in predeclared pkg sub but declared outside';
920 sub not_lexical6 { sub foo { } }
921 pass 'no crash when cloning a mysub declared inside an undef pack sub';
925 eval 'sub not_lexical7 { my @x }';
932 is ref \$x, 'SCALAR',
933 "redeffing a mysub's outside does not make it use the wrong pad"
939 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
940 prog => 'my sub foo; sub foo { foo } foo',
943 qr/Deep recursion on subroutine "foo"/,
944 'deep recursion warnings for lexical subs do not crash';
947 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
948 prog => 'my sub foo() { 42 } undef &foo',
951 qr/Constant subroutine foo undefined at /,
952 'constant undefinition warnings for lexical subs do not crash';
956 *AutoloadTestSuper::blah = \&foo;
957 sub AutoloadTestSuper::AUTOLOAD {
958 is $AutoloadTestSuper::AUTOLOAD, "AutoloadTestSuper::blah",
959 "Autoloading via inherited lex stub";
961 @AutoloadTest::ISA = AutoloadTestSuper::;
965 # This used to crash because op.c:find_lexical_cv was looking at the wrong
966 # CV’s OUTSIDE pointer. [perl #124099]
968 my sub h; sub{my $x; sub{h}}