10 sub a : lvalue { my $a = 34; ${\(bless \$a)} } # Return a temporary
11 sub b : lvalue { ${\shift} }
13 my $out = a(b()); # Check that temporaries are allowed.
14 is(ref $out, 'main'); # Not reached if error.
16 my @out = grep /main/, a(b()); # Check that temporaries are allowed.
17 cmp_ok(scalar @out, '==', 1); # Not reached if error.
21 # Check that we can return localized values from subroutines:
23 sub in : lvalue { $in = shift; }
24 sub neg : lvalue { #(num_str) return num_str
34 sub get_lex : lvalue { $in }
35 sub get_st : lvalue { $blah }
36 sub id : lvalue { ${\shift} }
37 sub id1 : lvalue { $_[0] }
38 sub inc : lvalue { ${\++$_[0]} }
45 cmp_ok($blah, '==', 7);
53 cmp_ok($blah, '==', 8);
61 cmp_ok($blah, '==', 10);
65 cmp_ok($in, '==', 10);
69 cmp_ok($blah, '==', 11);
73 cmp_ok($in, '==', 11);
77 cmp_ok($blah, '==', 20);
81 cmp_ok($in, '==', 20);
85 cmp_ok($blah, '==', 21);
89 cmp_ok($in, '==', 21);
93 cmp_ok($blah, '==', 22);
97 cmp_ok($in, '==', 22);
101 cmp_ok($blah, '==', 23);
105 cmp_ok($in, '==', 23);
107 ++inc(id1(id(get_st)));
109 cmp_ok($blah, '==', 25);
111 ++inc(id1(id(get_lex)));
113 cmp_ok($in, '==', 25);
117 $#c = 3; # These slots are not fillable.
119 # Explanation: empty slots contain &sv_undef.
121 =for disabled constructs
129 eval <<'EOE' or $_ = $@;
130 ($x, a3, $y, b2, $z, c4, $t) = (34 .. 78);
134 #@out = ($x, a3, $y, b2, $z, c4, $t);
135 #@in = (34 .. 41, (undef) x 4, 46);
136 #print "# '@out' ne '@in'\nnot " unless "@out" eq "@in";
138 like($_, qr/Can\'t return an uninitialized value from lvalue subroutine/);
146 sub a::var : lvalue { $var }
150 cmp_ok($var, '==', 45);
153 $o = bless \$oo, "a";
157 cmp_ok($var, '==', 47);
159 sub o : lvalue { $o }
163 cmp_ok($var, '==', 49);
165 sub nolv () { $x0, $x1 } # Not lvalue
169 eval <<'EOE' or $_ = $@;
174 like($_, qr/Can\'t modify non-lvalue subroutine call of &main::nolv in scalar assignment/);
178 eval <<'EOE' or $_ = $@;
183 like($_, qr/Can\'t modify non-lvalue subroutine call of &main::nolv in scalar assignment/);
187 eval <<'EOE' or $_ = $@;
192 like($_, qr/Can\'t modify non-lvalue subroutine call of &main::nolv in scalar assignment/);
194 $x0 = $x1 = $_ = undef;
197 eval <<'EOE' or $_ = $@;
198 $nolv->() = (2,3) if $_;
202 ok(!defined $_) or diag "'$_', '$x0', '$x1'";
204 $x0 = $x1 = $_ = undef;
207 eval <<'EOE' or $_ = $@;
212 like($_, qr/Can\'t modify non-lvalue subroutine call/)
213 or diag "'$_', '$x0', '$x1'";
216 sub rlv0 : lvalue { return }
219 eval <<'EOE' or $_ = $@;
224 like($_, qr/Can't return undef from lvalue subroutine/);
227 eval <<'EOE' or $_ = $@;
232 like($_, qr/Can't return undef from lvalue subroutine/,
233 'explicit return of nothing in scalar context');
236 eval <<'EOE' or $_ = $@;
241 ok(!defined $_) or diag $_;
244 eval <<'EOE' or $_ = $@;
249 ok(!defined $_, 'explicit return of nothing in list context') or diag $_;
252 (lv0($a,$b)) = (3,4);
253 is +($a//'undef') . ($b//'undef'), 'undefundef',
254 'list assignment to empty lvalue sub';
257 sub lv1u :lvalue { undef }
258 sub rlv1u :lvalue { undef }
261 eval <<'EOE' or $_ = $@;
266 like($_, qr/Can't return undef from lvalue subroutine/);
269 eval <<'EOE' or $_ = $@;
274 like($_, qr/Can't return undef from lvalue subroutine/,
275 'explicitly returning undef in scalar context');
278 eval <<'EOE' or $_ = $@;
283 ok(!defined, 'implicitly returning undef in list context');
286 eval <<'EOE' or $_ = $@;
291 ok(!defined, 'explicitly returning undef in list context');
296 eval <<'EOE' or $_ = $@;
297 sub lv1t : lvalue { index $x, 2 }
302 like($_, qr/Can\'t return a temporary from lvalue subroutine/);
305 eval <<'EOE' or $_ = $@;
306 sub rlv1t : lvalue { index $x, 2 }
311 like($_, qr/Can\'t return a temporary from lvalue subroutine/,
312 'returning a PADTMP explicitly');
315 eval <<'EOE' or $_ = $@;
320 like($_, qr/Can\'t return a temporary from lvalue subroutine/,
321 'returning a PADTMP explicitly (list context)');
323 # These next two tests are not necessarily normative. But this way we will
324 # know if this discrepancy changes.
327 eval <<'EOE' or $_ = $@;
328 sub scalarray : lvalue { @a || $b }
334 like($_, qr/Can\'t return a temporary from lvalue subroutine/,
335 'returning a scalar-context array via ||');
338 eval <<'EOE' or $_ = $@;
339 use warnings "FATAL" => "all";
340 sub myscalarray : lvalue { my @a = 1; @a || $b }
341 (myscalarray) = (2,3);
345 like($_, qr/Useless assignment to a temporary/,
346 'returning a scalar-context lexical array via ||');
349 sub lv2t : lvalue { shift }
354 sub xxx () { $xxx } # Not lvalue
357 eval <<'EOE' or $_ = $@;
358 sub lv1tmp : lvalue { xxx } # is it a TEMP?
363 like($_, qr/Can\'t modify non-lvalue subroutine call of &main::xxx at /);
366 eval <<'EOE' or $_ = $@;
371 like($_, qr/Can\'t modify non-lvalue subroutine call of &main::xxx at /);
373 sub yyy () { 'yyy' } # Const, not lvalue
376 eval <<'EOE' or $_ = $@;
377 sub lv1tmpr : lvalue { yyy } # is it read-only?
382 like($_, qr/Can\'t return a readonly value from lvalue subroutine at/);
385 eval <<'EOE' or $_ = $@;
390 like($_, qr/Can\'t return a readonly value from lvalue subroutine/);
393 sub lv2tmpr : lvalue { my $x = *foo; Internals::SvREADONLY $x, 1; $x }
397 like($@, qr/Can\'t return a readonly value from lvalue subroutine at/);
403 like($@, qr/Can\'t return a readonly value from lvalue subroutine/);
405 sub lva : lvalue {@a}
410 eval <<'EOE' or $_ = $@;
415 is("'@a' $_", "'2 3' ");
421 eval <<'EOE' or $_ = $@;
426 is("'@a' $_", "'2 3' ");
428 is lva->${\sub { return $_[0] }}, 2,
429 'lvalue->$thing when lvalue returns array';
431 my @my = qw/ a b c /;
432 sub lvmya : lvalue { @my }
434 is lvmya->${\sub { return $_[0] }}, 3,
435 'lvalue->$thing when lvalue returns lexical array';
437 sub lv1n : lvalue { $newvar }
440 eval <<'EOE' or $_ = $@;
445 is("'$newvar' $_", "'4' ");
447 sub lv1nn : lvalue { $nnewvar }
450 eval <<'EOE' or $_ = $@;
455 is("'$nnewvar' $_", "'3' ");
461 eval 'sub AUTOLOAD : lvalue { $newvar }';
465 # But autoloading should only be triggered by a call to an undefined
468 is $newvar, 12, 'AUTOLOAD does not take precedence over lvalue sub';
469 eval { &{"xxx"} = 14 };
470 is $newvar, 12, 'AUTOLOAD does not take precedence over non-lvalue sub';
474 sub alv : lvalue { $array[1] }
475 sub alv2 : lvalue { $array[$_[0]] }
476 sub hlv : lvalue { $hash{"foo"} }
477 sub hlv2 : lvalue { $hash{$_[0]} }
478 $array[1] = "not ok 51\n";
480 is(alv(), "ok 50\n");
482 alv2(20) = "ok 51\n";
483 is($array[20], "ok 51\n");
485 $hash{"foo"} = "not ok 52\n";
487 is($hash{foo}, "ok 52\n");
489 $hash{bar} = "not ok 53\n";
490 hlv("bar") = "ok 53\n";
491 is(hlv("bar"), "ok 53\n");
493 sub array : lvalue { @array }
494 sub array2 : lvalue { @array2 } # This is a global.
495 sub hash : lvalue { %hash }
496 sub hash2 : lvalue { %hash2 } # So's this.
497 @array2 = qw(foo bar);
498 %hash2 = qw(foo bar);
500 (array()) = qw(ok 54);
501 is("@array", "ok 54");
503 (array2()) = qw(ok 55);
504 is("@array2", "ok 55");
506 (hash()) = qw(ok 56);
507 cmp_ok($hash{ok}, '==', 56);
509 (hash2()) = qw(ok 57);
510 cmp_ok($hash2{ok}, '==', 57);
512 @array = qw(a b c d);
513 sub aslice1 : lvalue { @array[0,2] };
514 (aslice1()) = ("ok", "already");
515 is("@array", "ok b already d");
517 @array2 = qw(a B c d);
518 sub aslice2 : lvalue { @array2[0,2] };
519 (aslice2()) = ("ok", "already");
520 is("@array2", "ok B already d");
522 %hash = qw(a Alpha b Beta c Gamma);
523 sub hslice : lvalue { @hash{"c", "b"} }
524 (hslice()) = ("CISC", "BogoMIPS");
525 is(join("/",@hash{"c","a","b"}), "CISC/Alpha/BogoMIPS");
528 $str = "Hello, world!";
529 sub sstr : lvalue { substr($str, 1, 4) }
531 is($str, "Hi, world!");
533 $str = "Made w/ JavaScript";
534 sub veclv : lvalue { vec($str, 2, 32) }
536 veclv() = 0x5065726C;
539 veclv() = 0xD7859993;
541 is($str, "Made w/ PerlScript");
543 sub position : lvalue { pos }
545 $_ = "fee fi fo fum";
553 skip "no Hash::Util on miniperl", 3, if is_miniperl;
555 sub Hash::Util::bucket_ratio (\%);
557 sub keeze : lvalue { keys %__ }
560 is Hash::Util::bucket_ratio(%__), '1/64', 'keys assignment through lvalue sub';
561 eval { (keeze) = 64 };
562 like $@, qr/^Can't modify keys in list assignment at /,
563 'list assignment to keys through lv sub is forbidden';
564 sub akeeze : lvalue { keys @_ }
565 eval { (akeeze) = 64 };
566 like $@, qr/^Can't modify keys on array in list assignment at /,
567 'list assignment to keys @_ through lv sub is forbidden';
570 # Bug 20001223.002 (#5005): split thought that the list had only one element
572 sub lval1 : lvalue { $ary[0]; }
573 sub lval2 : lvalue { $ary[1]; }
574 (lval1(), lval2()) = split ' ', "1 2 3 4";
576 is(join(':', @ary), "1:2:6");
578 # check that an element of a tied hash/array can be assigned to via lvalueness
583 sub TIEHASH { bless \my $v => __PACKAGE__ }
584 sub STORE { ($key, $val) = @_[1,2] }
587 sub lval_tie_hash : lvalue {
588 tie my %t => 'Tie_Hash';
592 eval { lval_tie_hash() = "value"; };
594 is($@, "", "element of tied hash");
596 is("$Tie_Hash::key-$Tie_Hash::val", "key-value");
602 sub TIEARRAY { bless \my $v => __PACKAGE__ }
603 sub STORE { $val[ $_[1] ] = $_[2] }
606 sub lval_tie_array : lvalue {
607 tie my @t => 'Tie_Array';
611 eval { lval_tie_array() = "value"; };
614 is($@, "", "element of tied array");
616 is ($Tie_Array::val[0], "value");
619 # Check that tied pad vars that are returned can be assigned to
620 sub TIESCALAR { bless [] }
621 sub STORE {$wheel = $_[1]}
623 sub tied_pad_var :lvalue { tie my $tyre, ''; $tyre }
624 sub tied_pad_varr :lvalue { tie my $tyre, ''; return $tyre }
626 is $wheel, 1, 'tied pad var returned in scalar lvalue context';
627 tied_pad_var->${\sub{ $_[0] = 2 }};
628 is $wheel, 2, 'tied pad var returned in scalar ref context';
630 is $wheel, 3, 'tied pad var returned in list lvalue context';
631 $_ = 4 for tied_pad_var;
632 is $wheel, 4, 'tied pad var returned in list ref context';
634 is $wheel, 5, 'tied pad var explicitly returned in scalar lvalue context';
635 tied_pad_varr->${\sub{ $_[0] = 6 }};
636 is $wheel, 6, 'tied pad var explicitly returned in scalar ref context';
638 is $wheel, 7, 'tied pad var explicitly returned in list lvalue context';
639 $_ = 8 for tied_pad_varr;
640 is $wheel, 8, 'tied pad var explicitly returned in list ref context';
643 # Test explicit return of lvalue expression
645 # subs are copies from tests 1-~18 with an explicit return added.
646 # They used not to work, which is why they are ‘badly’ named.
647 sub bad_get_lex : lvalue { return $in };
648 sub bad_get_st : lvalue { return $blah }
650 sub bad_id : lvalue { return ${\shift} }
651 sub bad_id1 : lvalue { return $_[0] }
652 sub bad_inc : lvalue { return ${\++$_[0]} }
667 is($blah, 8, "yada");
670 cmp_ok($in, '==', 8);
672 bad_id(bad_get_st) = 10;
673 cmp_ok($blah, '==', 10);
675 bad_id(bad_get_lex) = 10;
676 cmp_ok($in, '==', 10);
678 ++bad_id(bad_get_st);
679 cmp_ok($blah, '==', 11);
681 ++bad_id(bad_get_lex);
682 cmp_ok($in, '==', 11);
684 bad_id1(bad_get_st) = 20;
685 cmp_ok($blah, '==', 20);
687 bad_id1(bad_get_lex) = 20;
688 cmp_ok($in, '==', 20);
690 ++bad_id1(bad_get_st);
691 cmp_ok($blah, '==', 21);
693 ++bad_id1(bad_get_lex);
694 cmp_ok($in, '==', 21);
697 cmp_ok($blah, '==', 22);
699 bad_inc(bad_get_lex);
700 cmp_ok($in, '==', 22);
702 bad_inc(bad_id(bad_get_st));
703 cmp_ok($blah, '==', 23);
705 bad_inc(bad_id(bad_get_lex));
706 cmp_ok($in, '==', 23);
708 ++bad_inc(bad_id1(bad_id(bad_get_st)));
709 cmp_ok($blah, '==', 25);
711 ++bad_inc(bad_id1(bad_id(bad_get_lex)));
712 cmp_ok($in, '==', 25);
718 my $depth = shift//0;
719 if ($depth == 2) { return $to_modify }
720 return &$r($depth+1);
723 is $to_modify, 7, 'recursive lvalue sub';
725 # Recursive with substr [perl #72706]
730 return &$pie($depth) if $depth--;
733 for my $depth (0, 1, 2) {
734 my $value = "Good $depth";
736 &$pie($depth) = $value;
738 is($@, '', "recursive lvalue substr return depth $depth");
740 "value assigned to recursive lvalue substr (depth $depth)");
745 my @arr = qw /one two three/;
747 sub lval_array () : lvalue {@arr}
753 is($line, "zeroonetwothree");
755 sub trythislval { scalar(@_)."x".join "", @_ }
756 is(trythislval(lval_array()), "3xonetwothree");
758 sub changeme { $_[2] = "free" }
759 changeme(lval_array);
760 is("@arr", "one two free");
762 # test again, with explicit return
763 sub rlval_array() : lvalue {return @arr}
764 @arr = qw /one two three/;
769 is($line, "zeroonetwothree");
770 is(trythislval(rlval_array()), "3xonetwothree");
771 changeme(rlval_array);
772 is("@arr", "one two free");
774 # Variations on the same theme, with multiple vars returned
776 sub lval_scalar_array () : lvalue { $scalar, @arr }
777 @arr = qw /one two three/;
779 for (lval_scalar_array) {
782 is($line, "zerohalfonetwothree");
783 is(trythislval(lval_scalar_array()), "4xhalfonetwothree");
784 changeme(lval_scalar_array);
785 is("@arr", "one free three");
787 sub lval_array_scalar () : lvalue { @arr, $scalar }
788 @arr = qw /one two three/;
791 for (lval_array_scalar) {
794 is($line, "zeroonetwothreefour");
795 is(trythislval(lval_array_scalar()), "4xonetwothreefour");
796 changeme(lval_array_scalar);
797 is("@arr", "one two free");
799 # Tests for specific ops not tested above
801 @array2 = qw 'one two free';
802 is join(',', map $_, sub:lvalue{@array2}->()), 'one,two,free',
803 'rv2av in reference context';
804 is join(',', map $_, sub:lvalue{@{\@array2}}->()), 'one,two,free',
805 'rv2av-with-ref in reference context';
807 my %hash = qw[a b c d];
808 like join(',', map $_, sub:lvalue{%hash}->()),
809 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'padhv in reference context';
811 %hash2 = qw[a b c d];
812 like join(',', map $_, sub:lvalue{%hash2}->()),
813 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'rv2hv in reference context';
814 like join(',', map $_, sub:lvalue{%{\%hash2}}->()),
815 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'rv2hv-with-ref in reference context';
820 sub AUTOLOAD :lvalue { *{$AUTOLOAD} };
822 my $foo = bless {},"Foo";
824 $foo->bar = sub { $result = "bar" };
826 is ($result, 'bar', "RT #41550");
830 skip 'no attributes.pm', 1 unless eval 'require attributes';
831 fresh_perl_is(<<'----', <<'====', {}, "lvalue can not be set after definition. [perl #68758]");
836 sub MODIFY_CODE_ATTRIBUTES {}
837 sub foo : lvalue : fr0g;
840 lvalue attribute ignored after the subroutine has been defined at - line 4.
841 lvalue attribute ignored after the subroutine has been defined at - line 6.
842 Can't modify non-lvalue subroutine call of &main::foo in scalar assignment at - line 7, near "3;"
843 Execution of - aborted due to compilation errors.
849 sub lval_decl : lvalue;
852 is($x, 5, "subroutine declared with lvalue before definition retains lvalue. [perl #68758]");
855 SKIP: { skip "no attributes.pm", 2 unless eval { require attributes };
856 sub utf8::valid :lvalue;
858 is "@{[ &attributes::get(\&utf8::valid) ]}", 'lvalue',
859 'sub declaration with :lvalue applies it to XSUBs';
861 BEGIN { *wonky = \&marjibberous }
863 is "@{[ &attributes::get(\&wonky) ]}", 'lvalue',
864 'sub declaration with :lvalue applies it to assigned stub';
867 sub fleen : lvalue { $pnare }
868 $pnare = __PACKAGE__;
869 ok eval { fleen = 1 }, "lvalues can return COWs (CATTLE?) [perl #75656]";\
870 is $pnare, 1, 'and returning CATTLE actually works';
871 $pnare = __PACKAGE__;
872 ok eval { (fleen) = 1 }, "lvalues can return COWs in list context";
873 is $pnare, 1, 'and returning COWs in list context actually works';
874 $pnare = __PACKAGE__;
875 ok eval { $_ = 1 for(fleen); 1 }, "lvalues can return COWs in ref cx";
876 is $pnare, 1, 'and returning COWs in reference context actually works';
879 # Returning an arbitrary expression, not necessarily lvalue
880 +sub :lvalue { return $ambaga || $ambaga }->() = 73;
881 is $ambaga, 73, 'explicit return of arbitrary expression (scalar context)';
882 (sub :lvalue { return $ambaga || $ambaga }->()) = 74;
883 is $ambaga, 74, 'explicit return of arbitrary expression (list context)';
884 +sub :lvalue { $ambaga || $ambaga }->() = 73;
885 is $ambaga, 73, 'implicit return of arbitrary expression (scalar context)';
886 (sub :lvalue { $ambaga || $ambaga }->()) = 74;
887 is $ambaga, 74, 'implicit return of arbitrary expression (list context)';
888 eval { +sub :lvalue { return 3 }->() = 4 };
889 like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
890 'assignment to numeric constant explicitly returned from lv sub';
891 eval { (sub :lvalue { return 3 }->()) = 4 };
892 like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
893 'assignment to num constant explicitly returned (list cx)';
894 eval { +sub :lvalue { 3 }->() = 4 };
895 like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
896 'assignment to numeric constant implicitly returned from lv sub';
897 eval { (sub :lvalue { 3 }->()) = 4 };
898 like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
899 'assignment to num constant implicitly returned (list cx)';
901 # reference (potential lvalue) context
903 for my $sub (sub :lvalue {$_}, sub :lvalue {return $_}) {
904 &$sub()->${\sub { $_[0] = 37 }};
905 is $_, '37', 'lvalue->method'.$suffix;
906 ${\scalar &$sub()} = 38;
907 is $_, '38', 'scalar(lvalue)'.$suffix;
908 sub assign39_with_proto ($) { $_[0] = 39 }
909 assign39_with_proto(&$sub());
910 is $_, '39', 'func(lvalue) when func has $ proto'.$suffix;
912 ${\(&$sub()||undef)} = 40;
913 is $_, '40', 'lvalue||...'.$suffix;
914 ${\(${\undef}||&$sub())} = 41; # extra ${\...} to bypass const folding
915 is $_, '41', '...||lvalue'.$suffix;
917 ${\(&$sub()&&undef)} = 42;
918 is $_, '42', 'lvalue&&...'.$suffix;
919 ${\(${\1}&&&$sub())} = 43;
920 is $_, '43', '...&&lvalue'.$suffix;
921 ${\(&$sub())[0]} = 44;
922 is $_, '44', '(lvalue)[0]'.$suffix;
924 continue { $suffix = ' (explicit return)' }
928 for my $sub (sub :lvalue {$_}, sub :lvalue {return $_}) {
931 is $_->[3], 4, 'func->[...] autovivification'.$suffix;
934 is $_->{3}, 4, 'func->{...} autovivification'.$suffix;
937 is $$_, 4, '${func()} autovivification' .$suffix;
940 is "@$_", 4, '@{func()} autovivification' .$suffix;
943 is join('-',%$_), '4-5', '%{func()} autovivification'.$suffix;
946 is $$_, 4, '${ (), func()} autovivification' .$suffix;
948 continue { $suffix = ' (explicit return)' }
950 # [perl #92406] [perl #92290] Returning a pad var in rvalue context
953 sub :lvalue { my $x = 72; $x },
954 sub :lvalue { my $x = 72; return $x }
956 is scalar(&$sub), 72, "sub returning pad var in scalar context$suffix";
957 is +(&$sub)[0], 72, "sub returning pad var in list context$suffix";
959 continue { $suffix = ' (explicit return)' }
961 # Returning read-only values in reference context
964 sub :lvalue { $] }->(),
965 sub :lvalue { return $] }->()
967 is \$_, \$], 'read-only values are returned in reference context'
968 .$suffix # (they used to be copied)
970 continue { $suffix = ' (explicit return)' }
972 # Returning unwritables from nested lvalue sub call in in rvalue context
973 # First, ensure we are testing what we think we are:
974 if (!Internals::SvREADONLY($])) { Internals::SvREADONLY($],1); }
975 sub squibble : lvalue { return $] }
976 sub squebble : lvalue { squibble }
977 sub squabble : lvalue { return squibble }
978 is $x = squebble, $], 'returning ro from nested lv sub call in rv cx';
979 is $x = squabble, $], 'explct. returning ro from nested lv sub in rv cx';
980 is \squebble, \$], 'returning ro from nested lv sub call in ref cx';
981 is \squabble, \$], 'explct. returning ro from nested lv sub in ref cx';
983 # [perl #102486] Sub calls as the last statement of an lvalue sub
987 sub strictlv :lvalue { use strict 'refs'; &$x }
988 sub lv :lvalue { &$x }
989 sub nonlv { ++$called }
991 ::like $@, qr/^Can't use string \("nonlv"\) as a subroutine ref while/,
992 'strict mode applies to sub:lvalue{ &$string }';
995 'sub:lvalue{&$x}->() does not die for non-lvalue inner sub call';
996 ::is $called, 1, 'The &$x actually called the sub';
997 eval { +sub :lvalue { &$x }->() = 3 };
998 ::like $@, qr/^Can't modify non-lvalue subroutine call of &_102486::nonlv at /,
999 'sub:lvalue{&$x}->() dies in true lvalue context';
1002 # TARG should be copied in rvalue context
1003 sub ucf :lvalue { ucfirst $_[0] }
1004 is ucf("just another ") . ucf("perl hacker,\n"),
1005 "Just another Perl hacker,\n", 'TARG is copied in rvalue scalar cx';
1006 is join('',ucf("just another "), ucf "perl hacker,\n"),
1007 "Just another Perl hacker,\n", 'TARG is copied in rvalue list cx';
1009 @_ ? ucfirst $_[0] : do {
1010 is ucfr("just another ") . ucfr("perl hacker,\n"),
1011 "Just another Perl hacker,\n",
1012 'TARG is copied in recursive rvalue scalar cx';
1013 is join('',ucfr("just another "), ucfr("perl hacker,\n")),
1014 "Just another Perl hacker,\n",
1015 'TARG is copied in recursive rvalue list cx';
1020 # Test TARG with potential lvalue context, too
1021 for (sub : lvalue { "$x" }->()) {
1022 is \$_, \$_, '\$_ == \$_ in for(sub :lvalue{"$x"}->()){...}'
1025 # [perl #117947] XSUBs should not be treated as lvalues at run time
1026 eval { &{\&utf8::is_utf8}("") = 3 };
1027 like $@, qr/^Can't modify non-lvalue subroutine call of &utf8::is_utf8 at /,
1028 'XSUB not seen at compile time dies in lvalue context';
1030 # [perl #119797] else implicitly returning value
1031 # This used to cause Bizarre copy of ARRAY in pp_leave
1032 sub else119797 : lvalue {
1034 1; # two statements force a leave op
1041 eval { (else119797(0)) = 1..3 };
1042 is $@, "", '$@ after writing to array returned by else';
1043 is "@119797", "1 2 3", 'writing to array returned by else';
1044 eval { (else119797(1)) = 4..6 };
1045 is $@, "", '$@ after writing to array returned by if (with else)';
1046 is "@119797", "4 5 6", 'writing to array returned by if (with else)';
1047 sub if119797 : lvalue {
1053 eval { (if119797(1)) = 4..6 };
1054 is $@, "", '$@ after writing to array returned by if';
1055 is "@119797", "4 5 6", 'writing to array returned by if';
1056 sub unless119797 : lvalue {
1062 eval { (unless119797(0)) = 4..6 };
1063 is $@, "", '$@ after writing to array returned by unless';
1064 is "@119797", "4 5 6", 'writing to array returned by unless';
1065 sub bare119797 : lvalue {
1071 eval { (bare119797(0)) = 4..6 };
1072 is $@, "", '$@ after writing to array returned by bare block';
1073 is "@119797", "4 5 6", 'writing to array returned by bare block';
1075 # a sub with nested scopes must pop rubbish on the stack
1078 sub loopreturn : lvalue {
1084 is($x, "b", "loopreturn");
1087 # a sub without nested scopes that still leaves rubbish on the stack
1088 # which needs popping
1091 sub junkreturn : lvalue {
1093 return $x unless $false and $false;
1097 is($x, "b", "junkreturn");