8 sub a : lvalue { my $a = 34; ${\(bless \$a)} } # Return a temporary
9 sub b : lvalue { ${\shift} }
11 my $out = a(b()); # Check that temporaries are allowed.
12 is(ref $out, 'main'); # Not reached if error.
14 my @out = grep /main/, a(b()); # Check that temporaries are allowed.
15 cmp_ok(scalar @out, '==', 1); # Not reached if error.
19 # Check that we can return localized values from subroutines:
21 sub in : lvalue { $in = shift; }
22 sub neg : lvalue { #(num_str) return num_str
32 sub get_lex : lvalue { $in }
33 sub get_st : lvalue { $blah }
34 sub id : lvalue { ${\shift} }
35 sub id1 : lvalue { $_[0] }
36 sub inc : lvalue { ${\++$_[0]} }
43 cmp_ok($blah, '==', 7);
51 cmp_ok($blah, '==', 8);
59 cmp_ok($blah, '==', 10);
63 cmp_ok($in, '==', 10);
67 cmp_ok($blah, '==', 11);
71 cmp_ok($in, '==', 11);
75 cmp_ok($blah, '==', 20);
79 cmp_ok($in, '==', 20);
83 cmp_ok($blah, '==', 21);
87 cmp_ok($in, '==', 21);
91 cmp_ok($blah, '==', 22);
95 cmp_ok($in, '==', 22);
99 cmp_ok($blah, '==', 23);
103 cmp_ok($in, '==', 23);
105 ++inc(id1(id(get_st)));
107 cmp_ok($blah, '==', 25);
109 ++inc(id1(id(get_lex)));
111 cmp_ok($in, '==', 25);
115 $#c = 3; # These slots are not fillable.
117 # Explanation: empty slots contain &sv_undef.
119 =for disabled constructs
127 eval <<'EOE' or $_ = $@;
128 ($x, a3, $y, b2, $z, c4, $t) = (34 .. 78);
132 #@out = ($x, a3, $y, b2, $z, c4, $t);
133 #@in = (34 .. 41, (undef) x 4, 46);
134 #print "# `@out' ne `@in'\nnot " unless "@out" eq "@in";
136 like($_, qr/Can\'t return an uninitialized value from lvalue subroutine/);
144 sub a::var : lvalue { $var }
148 cmp_ok($var, '==', 45);
151 $o = bless \$oo, "a";
155 cmp_ok($var, '==', 47);
157 sub o : lvalue { $o }
161 cmp_ok($var, '==', 49);
163 sub nolv () { $x0, $x1 } # Not lvalue
167 eval <<'EOE' or $_ = $@;
172 like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
176 eval <<'EOE' or $_ = $@;
181 like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
185 eval <<'EOE' or $_ = $@;
190 like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
192 $x0 = $x1 = $_ = undef;
195 eval <<'EOE' or $_ = $@;
196 $nolv->() = (2,3) if $_;
200 ok(!defined $_) or diag "'$_', '$x0', '$x1'";
202 $x0 = $x1 = $_ = undef;
205 eval <<'EOE' or $_ = $@;
210 like($_, qr/Can\'t modify non-lvalue subroutine call/)
211 or diag "'$_', '$x0', '$x1'";
213 sub lv0 : lvalue { } # Converted to lv10 in scalar context
216 eval <<'EOE' or $_ = $@;
221 like($_, qr/Can't return undef from lvalue subroutine/);
226 eval <<'EOE' or $_ = $@;
231 ok(!defined $_) or diag $_;
233 sub lv1u :lvalue { undef }
236 eval <<'EOE' or $_ = $@;
241 like($_, qr/Can't return undef from lvalue subroutine/);
244 eval <<'EOE' or $_ = $@;
249 # Fixed by change @10777
250 #print "# '$_'.\nnot "
251 # unless /Can\'t return an uninitialized value from lvalue subroutine/;
252 # print "ok 34 # Skip: removed test\n";
257 eval <<'EOE' or $_ = $@;
258 sub lv1t : lvalue { index $x, 2 }
263 like($_, qr/Can\'t modify index in lvalue subroutine return/);
266 eval <<'EOE' or $_ = $@;
267 sub lv2t : lvalue { shift }
272 like($_, qr/Can\'t modify shift in lvalue subroutine return/);
275 sub xxx () { $xxx } # Not lvalue
278 eval <<'EOE' or $_ = $@;
279 sub lv1tmp : lvalue { xxx } # is it a TEMP?
284 like($_, qr/Can\'t modify non-lvalue subroutine call in lvalue subroutine return/);
287 eval <<'EOE' or $_ = $@;
292 like($_, qr/Can\'t return a temporary from lvalue subroutine/);
294 sub yyy () { 'yyy' } # Const, not lvalue
297 eval <<'EOE' or $_ = $@;
298 sub lv1tmpr : lvalue { yyy } # is it read-only?
303 like($_, qr/Can\'t modify constant item in lvalue subroutine return/);
306 eval <<'EOE' or $_ = $@;
311 like($_, qr/Can\'t return a readonly value from lvalue subroutine/);
313 sub lva : lvalue {@a}
318 eval <<'EOE' or $_ = $@;
323 is("'@a' $_", "'2 3' ");
329 eval <<'EOE' or $_ = $@;
334 is("'@a' $_", "'2 3' ");
340 eval <<'EOE' or $_ = $@;
345 is("'@a' $_", "'2 3' ");
347 sub lv1n : lvalue { $newvar }
350 eval <<'EOE' or $_ = $@;
355 is("'$newvar' $_", "'4' ");
357 sub lv1nn : lvalue { $nnewvar }
360 eval <<'EOE' or $_ = $@;
365 is("'$nnewvar' $_", "'3' ");
371 eval 'sub AUTOLOAD : lvalue { $newvar }';
377 sub alv : lvalue { $array[1] }
378 sub alv2 : lvalue { $array[$_[0]] }
379 sub hlv : lvalue { $hash{"foo"} }
380 sub hlv2 : lvalue { $hash{$_[0]} }
381 $array[1] = "not ok 51\n";
383 is(alv(), "ok 50\n");
385 alv2(20) = "ok 51\n";
386 is($array[20], "ok 51\n");
388 $hash{"foo"} = "not ok 52\n";
390 is($hash{foo}, "ok 52\n");
392 $hash{bar} = "not ok 53\n";
393 hlv("bar") = "ok 53\n";
394 is(hlv("bar"), "ok 53\n");
396 sub array : lvalue { @array }
397 sub array2 : lvalue { @array2 } # This is a global.
398 sub hash : lvalue { %hash }
399 sub hash2 : lvalue { %hash2 } # So's this.
400 @array2 = qw(foo bar);
401 %hash2 = qw(foo bar);
403 (array()) = qw(ok 54);
404 is("@array", "ok 54");
406 (array2()) = qw(ok 55);
407 is("@array2", "ok 55");
409 (hash()) = qw(ok 56);
410 cmp_ok($hash{ok}, '==', 56);
412 (hash2()) = qw(ok 57);
413 cmp_ok($hash2{ok}, '==', 57);
415 @array = qw(a b c d);
416 sub aslice1 : lvalue { @array[0,2] };
417 (aslice1()) = ("ok", "already");
418 is("@array", "ok b already d");
420 @array2 = qw(a B c d);
421 sub aslice2 : lvalue { @array2[0,2] };
422 (aslice2()) = ("ok", "already");
423 is("@array2", "ok B already d");
425 %hash = qw(a Alpha b Beta c Gamma);
426 sub hslice : lvalue { @hash{"c", "b"} }
427 (hslice()) = ("CISC", "BogoMIPS");
428 is(join("/",@hash{"c","a","b"}), "CISC/Alpha/BogoMIPS");
431 $str = "Hello, world!";
432 sub sstr : lvalue { substr($str, 1, 4) }
434 is($str, "Hi, world!");
436 $str = "Made w/ JavaScript";
437 sub veclv : lvalue { vec($str, 2, 32) }
438 if (ord('A') != 193) {
439 veclv() = 0x5065726C;
442 veclv() = 0xD7859993;
444 is($str, "Made w/ PerlScript");
446 sub position : lvalue { pos }
448 $_ = "fee fi fo fum";
455 # Bug 20001223.002: split thought that the list had only one element
457 sub lval1 : lvalue { $ary[0]; }
458 sub lval2 : lvalue { $ary[1]; }
459 (lval1(), lval2()) = split ' ', "1 2 3 4";
461 is(join(':', @ary), "1:2:6");
463 # check that an element of a tied hash/array can be assigned to via lvalueness
468 sub TIEHASH { bless \my $v => __PACKAGE__ }
469 sub STORE { ($key, $val) = @_[1,2] }
472 sub lval_tie_hash : lvalue {
473 tie my %t => 'Tie_Hash';
477 eval { lval_tie_hash() = "value"; };
479 is($@, "", "element of tied hash");
481 is("$Tie_Hash::key-$Tie_Hash::val", "key-value");
487 sub TIEARRAY { bless \my $v => __PACKAGE__ }
488 sub STORE { $val[ $_[1] ] = $_[2] }
491 sub lval_tie_array : lvalue {
492 tie my @t => 'Tie_Array';
496 eval { lval_tie_array() = "value"; };
499 is($@, "", "element of tied array");
501 is ($Tie_Array::val[0], "value");
504 local $TODO = 'test explicit return of lval expr';
506 # subs are corrupted copies from tests 1-~4
507 sub bad_get_lex : lvalue { return $in };
508 sub bad_get_st : lvalue { return $blah }
510 sub bad_id : lvalue { return ${\shift} }
511 sub bad_id1 : lvalue { return $_[0] }
512 sub bad_inc : lvalue { return ${\++$_[0]} }
527 is($blah, 8, "yada");
531 local $TODO = "bug #23790";
532 my @arr = qw /one two three/;
534 sub lval_array () : lvalue {@arr}
540 is($line, "zeroonetwothree");
545 sub AUTOLOAD :lvalue { *{$AUTOLOAD} };
547 my $foo = bless {},"Foo";
549 $foo->bar = sub { $result = "bar" };
551 is ($result, 'bar', "RT #41550");
554 fresh_perl_is(<<'----', <<'====', "lvalue can not be set after definition. [perl #68758]");
561 lvalue attribute ignored after the subroutine has been defined at - line 4.
562 Can't modify non-lvalue subroutine call in scalar assignment at - line 5, near "3;"
563 Execution of - aborted due to compilation errors.
568 sub lval_decl : lvalue;
571 is($x, 5, "subroutine declared with lvalue before definition retains lvalue. [perl #68758]");
574 sub fleen : lvalue { $pnare }
575 $pnare = __PACKAGE__;
576 ok eval { fleen = 1 }, "lvalues can return COWs (CATTLE?) [perl #75656]";\
577 is $pnare, 1, 'and returning CATTLE actually works';
582 my ($word, $replace) = @_;
583 my $ref = \substr($word, 0, 1);
585 if ($replace eq "b") {
586 $result_3363 = $word;
591 a_3363($_, "v") for "test";
593 is($result_3363, "best", "ref-to-substr retains lvalue-ness under recursion [perl #3363]");