12 # -------------------- our -------------------- #
16 is foo, 42, 'calling our sub from same package';
17 is &foo, 42, 'calling our sub from same package (amper)';
20 is foo, 42, 'calling our sub from another package';
21 is &foo, 42, 'calling our sub from another package (amper)';
24 is foo, 43, 'our sub falling out of scope';
25 is &foo, 43, 'our sub falling out of scope (called via amper)';
32 is a, 43, 'our sub invisible inside itself';
33 is &a, 43, 'our sub invisible inside itself (called via amper)';
43 is b, 42, 'our sub visible inside itself after decl';
44 is &b, 42, 'our sub visible inside itself after decl (amper)';
55 is c, 42, 'our sub foo; makes lex alias for existing sub';
56 is &c, 42, 'our sub foo; makes lex alias for existing sub (amper)';
63 is eval ::d, 'd42', 'our sub foo; applies to subsequent sub foo {}';
67 is prototype "::e", '$', 'our sub with proto';
72 is $x, 42, 'lexical subs (even our) override all keywords';
75 is $y, 42, 'our subs from other packages override all keywords';
77 # Interaction with ‘use constant’
79 our sub const; # symtab now has an undefined CV
80 BEGIN { delete $::{const} } # delete symtab entry; pad entry still exists
81 use constant const => 3; # symtab now has a scalar ref
82 # inlining this used to fail an assertion (parentheses necessary):
83 is(const, 3, 'our sub pointing to "use constant" constant');
85 # our sub and method confusion
89 our sub h { ++$called; 4343 };
90 is((h F),4242, 'our sub symbol translation does not affect meth names');
93 print h F; # follows a different path through yylex to intuit_method
95 is $called, undef, 'our sub symbol translation & meth names after print'
99 =>, 'j', 'name_of_our_sub <newline> => is parsed properly';
100 sub _cmp { $a cmp $b }
101 sub bar::_cmp { $b cmp $a }
106 is join(" ", sort _cmp split //, 'oursub'), 'u u s r o b', 'sort our_sub'
109 # -------------------- state -------------------- #
111 use feature 'state'; # state
114 isnt \&::foo, \&foo, 'state sub is not stored in the package';
115 is foo, 44, 'calling state sub from same package';
116 is &foo, 44, 'calling state sub from same package (amper)';
118 is foo, 44, 'calling state sub from another package';
119 is &foo, 44, 'calling state sub from another package (amper)';
122 is foo, 43, 'state sub falling out of scope';
123 is &foo, 43, 'state sub falling out of scope (called via amper)';
128 is sa, 43, 'state sub invisible inside itself';
129 is &sa, 43, 'state sub invisible inside itself (called via amper)';
138 # ‘state sub foo{}’ creates a new pad entry, not reusing the forward
139 # declaration. Being invisible inside itself, it sees the stub.
141 like $@, qr/^Undefined subroutine &sb called at /,
142 'state sub foo {} after forward declaration';
144 like $@, qr/^Undefined subroutine &sb called at /,
145 'state sub foo {} after forward declaration (amper)';
155 is sb2, 44, 'state sub visible inside itself after decl';
156 is &sb2, 44, 'state sub visible inside itself after decl (amper)';
163 state sub sb3 { # new pad entry
164 # The sub containing this comment is invisible inside itself.
165 # So this one here will assign to the outer pad entry:
170 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
171 # Same test again, but inside an anonymous sub
180 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
187 like $@, qr/^Undefined subroutine &sc called at /,
188 'state sub foo; makes no lex alias for existing sub';
190 like $@, qr/^Undefined subroutine &sc called at /,
191 'state sub foo; makes no lex alias for existing sub (amper)';
196 is prototype eval{\&se}, '$', 'state sub with proto';
197 is prototype "se", undef, 'prototype "..." ignores state subs';
200 state sub if() { 44 }
202 is $x, 44, 'state subs override all keywords';
205 is $y, 44, 'state subs from other packages override all keywords';
208 use warnings; no warnings "experimental::lexical_subs";
210 local $SIG{__WARN__} = sub { $w .= shift };
211 eval '#line 87 squidges
216 '"state" subroutine &foo masks earlier declaration in same scope at '
217 . "squidges line 88.\n",
218 'warning for state sub masking earlier declaration';
220 # Since state vars inside anonymous subs are cloned at the same time as the
221 # anonymous subs containing them, the same should happen for state subs.
229 $sub1 = make_closure 48;
230 $sub2 = make_closure 49;
231 is &$sub1, 48, 'state sub in closure (1)';
232 is &$sub2, 49, 'state sub in closure (2)';
233 # But we need to test that state subs actually do persist from one invoca-
234 # tion of a named sub to another (i.e., that they are not my subs).
236 use warnings; no warnings "experimental::lexical_subs";
238 local $SIG{__WARN__} = sub { $w .= shift };
239 eval '#line 65 teetet
242 state sub poom { $x }
246 is $w, "Variable \"\$x\" will not stay shared at teetet line 67.\n",
247 'state subs get "Variable will not stay shared" messages';
249 my $poom2 = foom(678);
250 is eval{$poom->()}, eval {$poom2->()},
251 'state subs close over the first outer my var, like pkg subs';
254 state sub etetetet { $x }
255 is eval{etetetet}, 43, 'state sub ignores for() localisation';
258 # And we also need to test that multiple state subs can close over each
259 # other’s entries in the parent subs pad, and that cv_clone is not con-
261 sub make_anon_with_state_sub{
264 state sub s2 { \&s1 }
266 if (@_) { return \&s1 }
267 is s1,\&s2, 'state sub in anon closure closing over sibling state sub';
268 is s2,\&s1, 'state sub in anon closure closing over sibling state sub';
272 my $s = make_anon_with_state_sub;
275 # And make sure the state subs were actually cloned.
276 isnt make_anon_with_state_sub->(0), &$s(0),
277 'state subs in anon subs are cloned';
278 is &$s(0), &$s(0), 'but only when the anon sub is cloned';
280 # Check that nested state subs close over variables properly
293 }->(), 42, 'state sub with body defined in doubly-nested state subs';
305 }->(), 42, 'nested state subs declared in same scope';
307 local $SIG{__WARN__} = sub { $w .= shift };
308 use warnings 'closure';
316 like $w, qr/Variable \"\$x\" is not available at /,
317 "unavailability warning when state closure is defined in anon sub";
320 state sub BEGIN { exit };
321 pass 'state subs are never special blocks';
322 state sub END { shift }
323 is eval{END('jkqeudth')}, jkqeudth,
324 'state sub END {shift} implies @_, not @ARGV';
325 state sub CORE { scalar reverse shift }
326 is CORE::uc("hello"), "HELLO",
327 'lexical CORE does not interfere with CORE::...';
331 use warnings; no warnings "experimental::lexical_subs";
333 local $SIG{__WARN__} = sub { $w .= shift };
334 eval "#line 56 pygpyf\nsub redef {}";
335 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
336 "sub redefinition warnings from state subs";
340 is ref $_[0], 'ARRAY', 'state sub with proto';
344 state sub q () { 45 }
345 is q(), 45, 'state constant called with parens';
350 is x, 3, 'state sub defined inside eval';
354 if (@_) { # outer call
357 'state sub run-time redefinition applies to all recursion levels';
360 eval 'sub foo { 42 }';
366 switches => [ '-Mfeature=lexical_subs,state' ],
367 prog => 'state sub a { foo ref } a()',
371 'referencing a state sub after a syntax error does not crash';
375 state sub foo{ $stuff .= our $AUTOLOAD }
376 *A::AUTOLOAD = \&foo;
379 is $stuff, 'A::bar', 'state sub assigned to *AUTOLOAD can autoload';
382 state sub quire{qr "quires"}
383 package o { use overload qr => \&quire }
384 ok "quires" =~ bless([], o::), 'state sub used as overload method';
389 local *cvgv2 = *cvgv;
390 eval 'sub cvgv2 {42}'; # uses the stub already present
391 is foo, 42, 'defining state sub body via package sub declaration';
394 local $ENV{PERL5DB} = 'sub DB::DB{}';
397 switches => [ '-d' ],
398 progs => [ split "\n",
399 'use feature qw - lexical_subs state -;
400 no warnings q-experimental::lexical_subs-;
402 print qq|4\n| unless $DB::sub =~ DESTROY;
405 state sub foo {print qq|2\n|}
412 'state subs and DB::sub under -d'
416 switches => [ '-d' ],
417 progs => [ split "\n",
418 'use feature qw - lexical_subs state -;
419 no warnings q-experimental::lexical_subs-;
420 sub DB::goto{ print qq|4\n|; $_ = $DB::sub }
421 state sub foo {print qq|2\n|}
423 sub { goto &foo }->();
424 print $_ == \&foo ? qq|ok\n| : qq|$_\n|;
430 'state subs and DB::goto under -d'
433 # This used to fail an assertion, but only as a standalone script
434 is runperl(switches => ['-lXMfeature=:all'],
435 prog => 'state sub x {}; undef &x; print defined &x',
436 stderr => 1), "\n", 'undefining state sub';
438 state sub x { is +(caller 0)[3], 'x', 'state sub name in caller' }
442 state sub _cmp { $b cmp $a }
443 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
444 'sort state_sub LIST'
447 state sub handel { "" }
448 print handel, "ok ", curr_test(),
449 " - no 'No comma allowed' after state sub\n";
450 curr_test(curr_test()+1);
456 like $@, qr/^Undefined subroutine &φου called at /,
457 'state sub with utf8 name';
459 # This used to crash, but only as a standalone script
460 is runperl(switches => ['-lXMfeature=:all'],
461 prog => '$::x = global=>;
465 state sub x { print eval q|$x| }
469 stderr => 1), "42\n",
470 'closure behaviour of state sub in predeclared package sub';
472 # -------------------- my -------------------- #
476 isnt \&::foo, \&foo, 'my sub is not stored in the package';
477 is foo, 44, 'calling my sub from same package';
478 is &foo, 44, 'calling my sub from same package (amper)';
480 is foo, 44, 'calling my sub from another package';
481 is &foo, 44, 'calling my sub from another package (amper)';
484 is foo, 43, 'my sub falling out of scope';
485 is &foo, 43, 'my sub falling out of scope (called via amper)';
490 is ma, 43, 'my sub invisible inside itself';
491 is &ma, 43, 'my sub invisible inside itself (called via amper)';
500 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
501 # declaration. Being invisible inside itself, it sees the stub.
503 like $@, qr/^Undefined subroutine &mb called at /,
504 'my sub foo {} after forward declaration';
506 like $@, qr/^Undefined subroutine &mb called at /,
507 'my sub foo {} after forward declaration (amper)';
517 is mb2, 44, 'my sub visible inside itself after decl';
518 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
525 my sub mb3 { # new pad entry
526 # The sub containing this comment is invisible inside itself.
527 # So this one here will assign to the outer pad entry:
532 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
533 # Same test again, but inside an anonymous sub
542 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
549 like $@, qr/^Undefined subroutine &mc called at /,
550 'my sub foo; makes no lex alias for existing sub';
552 like $@, qr/^Undefined subroutine &mc called at /,
553 'my sub foo; makes no lex alias for existing sub (amper)';
558 is prototype eval{\&me}, '$', 'my sub with proto';
559 is prototype "me", undef, 'prototype "..." ignores my subs';
561 my $coderef = eval "my sub foo (\$\x{30cd}) {1}; \\&foo";
562 my $proto = prototype $coderef;
563 ok(utf8::is_utf8($proto), "my sub with UTF8 proto maintains the UTF8ness");
564 is($proto, "\$\x{30cd}", "check the prototypes actually match");
569 is $x, 44, 'my subs override all keywords';
572 is $y, 44, 'my subs from other packages override all keywords';
575 use warnings; no warnings "experimental::lexical_subs";
577 local $SIG{__WARN__} = sub { $w .= shift };
578 eval '#line 87 squidges
583 '"my" subroutine &foo masks earlier declaration in same scope at '
584 . "squidges line 88.\n",
585 'warning for my sub masking earlier declaration';
587 # Test that my subs are cloned inside anonymous subs.
595 $sub1 = mmake_closure 48;
596 $sub2 = mmake_closure 49;
597 is &$sub1, 48, 'my sub in closure (1)';
598 is &$sub2, 49, 'my sub in closure (2)';
599 # Test that they are cloned in named subs.
601 use warnings; no warnings "experimental::lexical_subs";
603 local $SIG{__WARN__} = sub { $w .= shift };
604 eval '#line 65 teetet
611 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
612 my $poom = mfoom(27);
613 my $poom2 = mfoom(678);
614 is $poom->(), 27, 'my subs closing over outer my var (1)';
615 is $poom2->(), 678, 'my subs closing over outer my var (2)';
619 my sub etetetet { $x }
621 is etetetet, 765, 'my sub respects for() localisation';
622 is aoeu, 43, 'unless it is declared outside the for loop';
625 # And we also need to test that multiple my subs can close over each
626 # other’s entries in the parent subs pad, and that cv_clone is not con-
628 sub make_anon_with_my_sub{
633 if (@_) { return eval { \&s1 } }
634 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
635 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
639 # Test my subs inside predeclared my subs
644 my sub s3 { eval '$x' }
647 is s2, 3, 'my sub inside predeclared my sub';
651 my $s = make_anon_with_my_sub;
654 # And make sure the my subs were actually cloned.
655 isnt make_anon_with_my_sub->(0), &$s(0),
656 'my subs in anon subs are cloned';
657 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
660 my sub BEGIN { exit };
661 pass 'my subs are never special blocks';
663 is END('jkqeudth'), jkqeudth,
664 'my sub END {shift} implies @_, not @ARGV';
668 use warnings; no warnings "experimental::lexical_subs";
670 local $SIG{__WARN__} = sub { $w .= shift };
671 eval "#line 56 pygpyf\nsub redef {}";
672 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
673 "sub redefinition warnings from my subs";
678 sub { eval "#line 87 khaki\n\\&x" }
680 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
681 "unavailability warning during compilation of eval in closure";
694 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
695 "unavailability warning during compilation of named sub in anon";
706 my($f,$l) = (__FILE__,__LINE__ - 1);
707 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
708 'unavailability warning during cloning';
710 is $@, "Undefined subroutine &x called at $f line $l.\n",
711 'Vivified sub is correctly named';
718 my $x = 'khaki car keys for the khaki car';
721 is $x, 'khaki car keys for the khaki car',
722 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
731 is ref $_[0], 'ARRAY', 'my sub with proto';
736 is q(), 46, 'my constant called with parens';
741 sub x { x() if $count++ < 10 }
743 is $count, 11, 'my recursive subs';
748 is x, 3, 'my sub defined inside eval';
751 BEGIN { eval 'sub z {4}' }
752 is z, 4, 'my sub defined in BEGIN { eval "..." }';
757 local $SIG{__WARN__} = sub { $w .= shift };
758 eval q{ my sub george () { 2 } };
759 is $w, undef, 'no double free from constant my subs';
762 switches => [ '-Mfeature=lexical_subs,state' ],
763 prog => 'my sub a { foo ref } a()',
767 'referencing a my sub after a syntax error does not crash';
771 my sub foo{ $stuff .= our $AUTOLOAD }
772 *A::AUTOLOAD = \&foo;
775 is $stuff, 'A::bar', 'my sub assigned to *AUTOLOAD can autoload';
778 my sub quire{qr "quires"}
779 package mo { use overload qr => \&quire }
780 ok "quires" =~ bless([], mo::), 'my sub used as overload method';
785 local *mcvgv2 = *mcvgv;
786 eval 'sub mcvgv2 {42}'; # uses the stub already present
787 is foo, 42, 'defining my sub body via package sub declaration';
792 local *mcvgv4 = *mcvgv3;
793 eval 'sub mcvgv4 {42}'; # uses the stub already present
794 undef *mcvgv3; undef *mcvgv4; # leaves the pad with the only reference
796 # We would have crashed by now if it weren’t fixed.
797 pass "pad taking ownership once more of packagified my-sub";
800 local $ENV{PERL5DB} = 'sub DB::DB{}';
803 switches => [ '-d' ],
804 progs => [ split "\n",
805 'use feature qw - lexical_subs state -;
806 no warnings q-experimental::lexical_subs-;
808 print qq|4\n| unless $DB::sub =~ DESTROY;
811 my sub foo {print qq|2\n|}
818 'my subs and DB::sub under -d'
821 # This used to fail an assertion, but only as a standalone script
822 is runperl(switches => ['-lXMfeature=:all'],
823 prog => 'my sub x {}; undef &x; print defined &x',
824 stderr => 1), "\n", 'undefining my sub';
826 my sub x { is +(caller 0)[3], 'x', 'my sub name in caller' }
830 my sub _cmp { $b cmp $a }
831 is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
836 print handel,"ok ",curr_test()," - no 'No comma allowed' after my sub\n";
837 curr_test(curr_test()+1);
841 my sub y :prototype() {$x};
842 is y, 43, 'my sub that looks like constant closure';
848 like $@, qr/^Undefined subroutine &φου called at /,
849 'my sub with utf8 name';
853 local $SIG{__WARN__} = sub { $w = shift };
854 use warnings 'closure';
855 eval 'sub stayshared { my sub x; sub notstayshared { x } } 1' or die;
856 like $w, qr/^Subroutine "&x" will not stay shared at /,
857 'Subroutine will not stay shared';
860 # -------------------- Interactions (and misc tests) -------------------- #
865 sub s1 { state sub foo { \&s2 } foo }
867 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
871 sub not_lexical { state sub foo { \&s2 } foo }
872 is not_lexical->(), 3, 'state subs that reference my sub from outside';
875 # Test my subs inside predeclared package subs
876 # This test also checks that CvOUTSIDE pointers are not mangled when the
877 # inner sub’s CvOUTSIDE points to another sub.
888 is not_lexical3, 23, 'my subs inside predeclared package subs';
890 # Test my subs inside predeclared package sub, where the lexical sub is
891 # declared outside the package sub.
892 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
893 # not declared inside the sub that its CvOUTSIDE points to.
904 is not_lexical4, 234,
905 'my sub defined in predeclared pkg sub but declared outside';
910 sub not_lexical6 { sub foo { } }
911 pass 'no crash when cloning a mysub declared inside an undef pack sub';
915 eval 'sub not_lexical7 { my @x }';
922 is ref \$x, 'SCALAR',
923 "redeffing a mysub's outside does not make it use the wrong pad"
929 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
930 prog => 'my sub foo; sub foo { foo } foo',
933 qr/Deep recursion on subroutine "foo"/,
934 'deep recursion warnings for lexical subs do not crash';
937 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
938 prog => 'my sub foo() { 42 } undef &foo',
941 qr/Constant subroutine foo undefined at /,
942 'constant undefinition warnings for lexical subs do not crash';
946 *AutoloadTestSuper::blah = \&foo;
947 sub AutoloadTestSuper::AUTOLOAD {
948 is $AutoloadTestSuper::AUTOLOAD, "AutoloadTestSuper::blah",
949 "Autoloading via inherited lex stub";
951 @AutoloadTest::ISA = AutoloadTestSuper::;
955 # This used to crash because op.c:find_lexical_cv was looking at the wrong
956 # CV’s OUTSIDE pointer. [perl #124099]
958 my sub h; sub{my $x; sub{h}}
961 is join("-", qw(aa bb), do { my sub lleexx; 123 }, qw(cc dd)),
962 "aa-bb-123-cc-dd", 'do { my sub...} in a list [perl #132442]';