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');
100 # -------------------- state -------------------- #
102 use feature 'state'; # state
105 isnt \&::foo, \&foo, 'state sub is not stored in the package';
106 is eval foo, 44, 'calling state sub from same package';
107 is eval &foo, 44, 'calling state sub from same package (amper)';
109 is eval foo, 44, 'calling state sub from another package';
110 is eval &foo, 44, 'calling state sub from another package (amper)';
113 is foo, 43, 'state sub falling out of scope';
114 is &foo, 43, 'state sub falling out of scope (called via amper)';
119 is sa, 43, 'state sub invisible inside itself';
120 is &sa, 43, 'state sub invisible inside itself (called via amper)';
129 # ‘state sub foo{}’ creates a new pad entry, not reusing the forward
130 # declaration. Being invisible inside itself, it sees the stub.
132 like $@, qr/^Undefined subroutine &sb called at /,
133 'state sub foo {} after forward declaration';
135 like $@, qr/^Undefined subroutine &sb called at /,
136 'state sub foo {} after forward declaration (amper)';
146 is sb2, 44, 'state sub visible inside itself after decl';
147 is &sb2, 44, 'state sub visible inside itself after decl (amper)';
154 state sub sb3 { # new pad entry
155 # The sub containing this comment is invisible inside itself.
156 # So this one here will assign to the outer pad entry:
161 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
162 # Same test again, but inside an anonymous sub
171 'sub foo{} applying to "state sub foo;" even inside state sub foo{}';
178 like $@, qr/^Undefined subroutine &sc called at /,
179 'state sub foo; makes no lex alias for existing sub';
181 like $@, qr/^Undefined subroutine &sc called at /,
182 'state sub foo; makes no lex alias for existing sub (amper)';
187 is prototype eval{\&se}, '$', 'state sub with proto';
188 is prototype "se", undef, 'prototype "..." ignores state subs';
191 state sub if() { 44 }
193 is $x, 44, 'state subs override all keywords';
196 is $y, 44, 'state subs from other packages override all keywords';
199 use warnings; no warnings "experimental::lexical_subs";
201 local $SIG{__WARN__} = sub { $w .= shift };
202 eval '#line 87 squidges
207 '"state" subroutine &foo masks earlier declaration in same scope at '
208 . "squidges line 88.\n",
209 'warning for state sub masking earlier declaration';
211 # Since state vars inside anonymous subs are cloned at the same time as the
212 # anonymous subs containing them, the same should happen for state subs.
220 $sub1 = make_closure 48;
221 $sub2 = make_closure 49;
222 is &$sub1, 48, 'state sub in closure (1)';
223 is &$sub2, 49, 'state sub in closure (2)';
224 # But we need to test that state subs actually do persist from one invoca-
225 # tion of a named sub to another (i.e., that they are not my subs).
227 use warnings; no warnings "experimental::lexical_subs";
229 local $SIG{__WARN__} = sub { $w .= shift };
230 eval '#line 65 teetet
233 state sub poom { $x }
237 is $w, "Variable \"\$x\" will not stay shared at teetet line 67.\n",
238 'state subs get "Variable will not stay shared" messages';
240 my $poom2 = foom(678);
241 is eval{$poom->()}, eval {$poom2->()},
242 'state subs close over the first outer my var, like pkg subs';
245 state sub etetetet { $x }
246 is eval{etetetet}, 43, 'state sub ignores for() localisation';
249 # And we also need to test that multiple state subs can close over each
250 # other’s entries in the parent subs pad, and that cv_clone is not con-
252 sub make_anon_with_state_sub{
255 state sub s2 { \&s1 }
257 if (@_) { return \&s1 }
258 is s1,\&s2, 'state sub in anon closure closing over sibling state sub';
259 is s2,\&s1, 'state sub in anon closure closing over sibling state sub';
263 my $s = make_anon_with_state_sub;
266 # And make sure the state subs were actually cloned.
267 isnt make_anon_with_state_sub->(0), &$s(0),
268 'state subs in anon subs are cloned';
269 is &$s(0), &$s(0), 'but only when the anon sub is cloned';
272 state sub BEGIN { exit };
273 pass 'state subs are never special blocks';
274 state sub END { shift }
275 is eval{END('jkqeudth')}, jkqeudth,
276 'state sub END {shift} implies @_, not @ARGV';
277 state sub CORE { scalar reverse shift }
278 is CORE::uc("hello"), "HELLO",
279 'lexical CORE does not interfere with CORE::...';
283 use warnings; no warnings "experimental::lexical_subs";
285 local $SIG{__WARN__} = sub { $w .= shift };
286 eval "#line 56 pygpyf\nsub redef {}";
287 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
288 "sub redefinition warnings from state subs";
292 is ref $_[0], 'ARRAY', 'state sub with proto';
296 state sub q () { 45 }
297 is q(), 45, 'state constant called with parens';
302 is x, 3, 'state sub defined inside eval';
306 if (@_) { # outer call
309 'state sub run-time redefinition applies to all recursion levels';
312 eval 'sub foo { 42 }';
318 switches => [ '-Mfeature=lexical_subs,state' ],
319 prog => 'state sub a { foo ref } a()',
323 'referencing a state sub after a syntax error does not crash';
327 state sub foo{ $stuff .= our $AUTOLOAD }
328 *A::AUTOLOAD = \&foo;
331 is $stuff, 'A::bar', 'state sub assigned to *AUTOLOAD can autoload';
334 state sub quire{qr "quires"}
335 package o { use overload qr => \&quire }
336 ok "quires" =~ bless([], o::), 'state sub used as overload method';
341 local *cvgv2 = *cvgv;
342 eval 'sub cvgv2 {42}'; # uses the stub already present
343 is foo, 42, 'defining state sub body via package sub declaration';
346 local $ENV{PERL5DB} = 'sub DB::DB{}';
349 switches => [ '-d' ],
350 progs => [ split "\n",
351 'use feature qw - lexical_subs state -;
352 no warnings q-experimental::lexical_subs-;
353 sub DB::sub{ print qq|4\n|; goto $DB::sub }
354 state sub foo {print qq|2\n|}
361 'state subs and DB::sub under -d'
365 # -------------------- my -------------------- #
369 isnt \&::foo, \&foo, 'my sub is not stored in the package';
370 is foo, 44, 'calling my sub from same package';
371 is &foo, 44, 'calling my sub from same package (amper)';
373 is foo, 44, 'calling my sub from another package';
374 is &foo, 44, 'calling my sub from another package (amper)';
377 is foo, 43, 'my sub falling out of scope';
378 is &foo, 43, 'my sub falling out of scope (called via amper)';
383 is ma, 43, 'my sub invisible inside itself';
384 is &ma, 43, 'my sub invisible inside itself (called via amper)';
393 # ‘my sub foo{}’ creates a new pad entry, not reusing the forward
394 # declaration. Being invisible inside itself, it sees the stub.
396 like $@, qr/^Undefined subroutine &mb called at /,
397 'my sub foo {} after forward declaration';
399 like $@, qr/^Undefined subroutine &mb called at /,
400 'my sub foo {} after forward declaration (amper)';
410 is mb2, 44, 'my sub visible inside itself after decl';
411 is &mb2, 44, 'my sub visible inside itself after decl (amper)';
418 my sub mb3 { # new pad entry
419 # The sub containing this comment is invisible inside itself.
420 # So this one here will assign to the outer pad entry:
425 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
426 # Same test again, but inside an anonymous sub
435 'sub foo{} applying to "my sub foo;" even inside my sub foo{}';
442 like $@, qr/^Undefined subroutine &mc called at /,
443 'my sub foo; makes no lex alias for existing sub';
445 like $@, qr/^Undefined subroutine &mc called at /,
446 'my sub foo; makes no lex alias for existing sub (amper)';
451 is prototype eval{\&me}, '$', 'my sub with proto';
452 is prototype "me", undef, 'prototype "..." ignores my subs';
454 my $coderef = eval "my sub foo (\$\x{30cd}) {1}; \\&foo";
455 my $proto = prototype $coderef;
456 ok(utf8::is_utf8($proto), "my sub with UTF8 proto maintains the UTF8ness");
457 is($proto, "\$\x{30cd}", "check the prototypes actually match");
462 is $x, 44, 'my subs override all keywords';
465 is $y, 44, 'my subs from other packages override all keywords';
468 use warnings; no warnings "experimental::lexical_subs";
470 local $SIG{__WARN__} = sub { $w .= shift };
471 eval '#line 87 squidges
476 '"my" subroutine &foo masks earlier declaration in same scope at '
477 . "squidges line 88.\n",
478 'warning for my sub masking earlier declaration';
480 # Test that my subs are cloned inside anonymous subs.
488 $sub1 = mmake_closure 48;
489 $sub2 = mmake_closure 49;
490 is &$sub1, 48, 'my sub in closure (1)';
491 is &$sub2, 49, 'my sub in closure (2)';
492 # Test that they are cloned in named subs.
494 use warnings; no warnings "experimental::lexical_subs";
496 local $SIG{__WARN__} = sub { $w .= shift };
497 eval '#line 65 teetet
504 is $w, undef, 'my subs get no "Variable will not stay shared" messages';
505 my $poom = mfoom(27);
506 my $poom2 = mfoom(678);
507 is $poom->(), 27, 'my subs closing over outer my var (1)';
508 is $poom2->(), 678, 'my subs closing over outer my var (2)';
512 my sub etetetet { $x }
514 is etetetet, 765, 'my sub respects for() localisation';
515 is aoeu, 43, 'unless it is declared outside the for loop';
518 # And we also need to test that multiple my subs can close over each
519 # other’s entries in the parent subs pad, and that cv_clone is not con-
521 sub make_anon_with_my_sub{
526 if (@_) { return eval { \&s1 } }
527 is eval{s1},eval{\&s2}, 'my sub in anon closure closing over sibling my sub';
528 is eval{s2},eval{\&s1}, 'my sub in anon closure closing over sibling my sub';
532 # Test my subs inside predeclared my subs
537 my sub s3 { eval '$x' }
540 is s2, 3, 'my sub inside predeclared my sub';
544 my $s = make_anon_with_my_sub;
547 # And make sure the my subs were actually cloned.
548 isnt make_anon_with_my_sub->(0), &$s(0),
549 'my subs in anon subs are cloned';
550 isnt &$s(0), &$s(0), 'at each invocation of the enclosing sub';
553 my sub BEGIN { exit };
554 pass 'my subs are never special blocks';
556 is END('jkqeudth'), jkqeudth,
557 'my sub END {shift} implies @_, not @ARGV';
561 use warnings; no warnings "experimental::lexical_subs";
563 local $SIG{__WARN__} = sub { $w .= shift };
564 eval "#line 56 pygpyf\nsub redef {}";
565 is $w, "Subroutine redef redefined at pygpyf line 56.\n",
566 "sub redefinition warnings from my subs";
571 sub { eval "#line 87 khaki\n\\&x" }
573 is $w, "Subroutine \"&x\" is not available at khaki line 87.\n",
574 "unavailability warning during compilation of eval in closure";
587 is $w, "Subroutine \"&x\" is not available at khaki line 90.\n",
588 "unavailability warning during compilation of named sub in anon";
599 my($f,$l) = (__FILE__,__LINE__ - 1);
600 is $w, "Subroutine \"&x\" is not available at $f line $l.\n",
601 'unavailability warning during cloning';
603 is $@, "Undefined subroutine &x called at $f line $l.\n",
604 'Vivified sub is correctly named';
611 my $x = 'khaki car keys for the khaki car';
614 is $x, 'khaki car keys for the khaki car',
615 'mysubs in inner clonables use the running clone of their CvOUTSIDE'
624 is ref $_[0], 'ARRAY', 'my sub with proto';
629 is q(), 46, 'my constant called with parens';
634 sub x { x() if $count++ < 10 }
636 is $count, 11, 'my recursive subs';
641 is x, 3, 'my sub defined inside eval';
646 local $SIG{__WARN__} = sub { $w .= shift };
647 eval q{ my sub george () { 2 } };
648 is $w, undef, 'no double free from constant my subs';
651 switches => [ '-Mfeature=lexical_subs,state' ],
652 prog => 'my sub a { foo ref } a()',
656 'referencing a my sub after a syntax error does not crash';
660 my sub foo{ $stuff .= our $AUTOLOAD }
661 *A::AUTOLOAD = \&foo;
664 is $stuff, 'A::bar', 'my sub assigned to *AUTOLOAD can autoload';
667 my sub quire{qr "quires"}
668 package mo { use overload qr => \&quire }
669 ok "quires" =~ bless([], mo::), 'my sub used as overload method';
674 local *mcvgv2 = *mcvgv;
675 eval 'sub mcvgv2 {42}'; # uses the stub already present
676 is foo, 42, 'defining my sub body via package sub declaration';
681 local *mcvgv4 = *mcvgv3;
682 eval 'sub mcvgv4 {42}'; # uses the stub already present
683 undef *mcvgv3; undef *mcvgv4; # leaves the pad with the only reference
685 # We would have crashed by now if it weren’t fixed.
686 pass "pad taking ownership once more of packagified my-sub";
689 local $ENV{PERL5DB} = 'sub DB::DB{}';
692 switches => [ '-d' ],
693 progs => [ split "\n",
694 'use feature qw - lexical_subs state -;
695 no warnings q-experimental::lexical_subs-;
696 sub DB::sub{ print qq|4\n|; goto $DB::sub }
697 my sub foo {print qq|2\n|}
704 'my subs and DB::sub under -d'
708 # -------------------- Interactions (and misc tests) -------------------- #
713 sub s1 { state sub foo { \&s2 } foo }
715 }->()(), 3, 'state sub inside my sub closing over my sub uncle';
719 sub not_lexical { state sub foo { \&s2 } foo }
720 is not_lexical->(), 3, 'state subs that reference my sub from outside';
723 # Test my subs inside predeclared package subs
724 # This test also checks that CvOUTSIDE pointers are not mangled when the
725 # inner sub’s CvOUTSIDE points to another sub.
736 is not_lexical3, 23, 'my subs inside predeclared package subs';
738 # Test my subs inside predeclared package sub, where the lexical sub is
739 # declared outside the package sub.
740 # This checks that CvOUTSIDE pointers are fixed up even when the sub is
741 # not declared inside the sub that its CvOUTSIDE points to.
752 is not_lexical4, 234,
753 'my sub defined in predeclared pkg sub but declared outside';
758 sub not_lexical6 { sub foo { } }
759 pass 'no crash when cloning a mysub declared inside an undef pack sub';
763 eval 'sub not_lexical7 { my @x }';
770 is ref \$x, 'SCALAR',
771 "redeffing a mysub's outside does not make it use the wrong pad"
777 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
778 prog => 'my sub foo; sub foo { foo } foo',
781 qr/Deep recursion on subroutine "foo"/,
782 'deep recursion warnings for lexical subs do not crash';
785 switches => [ '-Mfeature=lexical_subs,state', '-Mwarnings=FATAL,all', '-M-warnings=experimental::lexical_subs' ],
786 prog => 'my sub foo() { 42 } undef &foo',
789 qr/Constant subroutine foo undefined at /,
790 'constant undefinition warnings for lexical subs do not crash';
794 *AutoloadTestSuper::blah = \&foo;
795 sub AutoloadTestSuper::AUTOLOAD {
796 is $AutoloadTestSuper::AUTOLOAD, "AutoloadTestSuper::blah",
797 "Autoloading via inherited lex stub";
799 @AutoloadTest::ISA = AutoloadTestSuper::;