This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
skip failing leak test under -Dmad
[perl5.git] / t / op / sub_lval.t
CommitLineData
cd06dffe
GS
1BEGIN {
2 chdir 't' if -d 't';
20822f61 3 @INC = '../lib';
cb949c37 4 require './test.pl';
cd06dffe 5}
5811c07e 6plan tests=>191;
cd06dffe 7
78f9721b
SM
8sub a : lvalue { my $a = 34; ${\(bless \$a)} } # Return a temporary
9sub b : lvalue { ${\shift} }
cd06dffe
GS
10
11my $out = a(b()); # Check that temporaries are allowed.
cb949c37 12is(ref $out, 'main'); # Not reached if error.
cd06dffe
GS
13
14my @out = grep /main/, a(b()); # Check that temporaries are allowed.
cb949c37 15cmp_ok(scalar @out, '==', 1); # Not reached if error.
cd06dffe
GS
16
17my $in;
18
19# Check that we can return localized values from subroutines:
20
a98df962
GS
21sub in : lvalue { $in = shift; }
22sub neg : lvalue { #(num_str) return num_str
cd06dffe
GS
23 local $_ = shift;
24 s/^\+/-/;
25 $_;
26}
27in(neg("+2"));
28
29
cb949c37 30is($in, '-2');
cd06dffe 31
a98df962
GS
32sub get_lex : lvalue { $in }
33sub get_st : lvalue { $blah }
78f9721b 34sub id : lvalue { ${\shift} }
a98df962 35sub id1 : lvalue { $_[0] }
78f9721b 36sub inc : lvalue { ${\++$_[0]} }
cd06dffe
GS
37
38$in = 5;
39$blah = 3;
40
41get_st = 7;
42
cb949c37 43cmp_ok($blah, '==', 7);
cd06dffe
GS
44
45get_lex = 7;
46
cb949c37 47cmp_ok($in, '==', 7);
cd06dffe
GS
48
49++get_st;
50
cb949c37 51cmp_ok($blah, '==', 8);
cd06dffe
GS
52
53++get_lex;
54
cb949c37 55cmp_ok($in, '==', 8);
cd06dffe
GS
56
57id(get_st) = 10;
58
cb949c37 59cmp_ok($blah, '==', 10);
cd06dffe
GS
60
61id(get_lex) = 10;
62
cb949c37 63cmp_ok($in, '==', 10);
cd06dffe
GS
64
65++id(get_st);
66
cb949c37 67cmp_ok($blah, '==', 11);
cd06dffe
GS
68
69++id(get_lex);
70
cb949c37 71cmp_ok($in, '==', 11);
cd06dffe
GS
72
73id1(get_st) = 20;
74
cb949c37 75cmp_ok($blah, '==', 20);
cd06dffe
GS
76
77id1(get_lex) = 20;
78
cb949c37 79cmp_ok($in, '==', 20);
cd06dffe
GS
80
81++id1(get_st);
82
cb949c37 83cmp_ok($blah, '==', 21);
cd06dffe
GS
84
85++id1(get_lex);
86
cb949c37 87cmp_ok($in, '==', 21);
cd06dffe
GS
88
89inc(get_st);
90
cb949c37 91cmp_ok($blah, '==', 22);
cd06dffe
GS
92
93inc(get_lex);
94
cb949c37 95cmp_ok($in, '==', 22);
cd06dffe
GS
96
97inc(id(get_st));
98
cb949c37 99cmp_ok($blah, '==', 23);
cd06dffe
GS
100
101inc(id(get_lex));
102
cb949c37 103cmp_ok($in, '==', 23);
cd06dffe
GS
104
105++inc(id1(id(get_st)));
106
cb949c37 107cmp_ok($blah, '==', 25);
cd06dffe
GS
108
109++inc(id1(id(get_lex)));
110
cb949c37 111cmp_ok($in, '==', 25);
cd06dffe
GS
112
113@a = (1) x 3;
114@b = (undef) x 2;
115$#c = 3; # These slots are not fillable.
116
117# Explanation: empty slots contain &sv_undef.
118
119=for disabled constructs
120
a98df962
GS
121sub a3 :lvalue {@a}
122sub b2 : lvalue {@b}
123sub c4: lvalue {@c}
cd06dffe
GS
124
125$_ = '';
126
127eval <<'EOE' or $_ = $@;
128 ($x, a3, $y, b2, $z, c4, $t) = (34 .. 78);
129 1;
130EOE
131
132#@out = ($x, a3, $y, b2, $z, c4, $t);
133#@in = (34 .. 41, (undef) x 4, 46);
2fe1f0f5 134#print "# '@out' ne '@in'\nnot " unless "@out" eq "@in";
cd06dffe 135
cb949c37
NC
136like($_, qr/Can\'t return an uninitialized value from lvalue subroutine/);
137print "ok 22\n";
138
cd06dffe
GS
139=cut
140
cd06dffe
GS
141
142my $var;
143
a98df962 144sub a::var : lvalue { $var }
cd06dffe
GS
145
146"a"->var = 45;
147
cb949c37 148cmp_ok($var, '==', 45);
cd06dffe
GS
149
150my $oo;
151$o = bless \$oo, "a";
152
153$o->var = 47;
154
cb949c37 155cmp_ok($var, '==', 47);
cd06dffe 156
a98df962 157sub o : lvalue { $o }
cd06dffe
GS
158
159o->var = 49;
160
cb949c37 161cmp_ok($var, '==', 49);
cd06dffe
GS
162
163sub nolv () { $x0, $x1 } # Not lvalue
164
165$_ = '';
166
167eval <<'EOE' or $_ = $@;
168 nolv = (2,3);
169 1;
170EOE
171
cb949c37 172like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
cd06dffe
GS
173
174$_ = '';
175
176eval <<'EOE' or $_ = $@;
177 nolv = (2,3) if $_;
178 1;
179EOE
180
cb949c37 181like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
cd06dffe
GS
182
183$_ = '';
184
185eval <<'EOE' or $_ = $@;
186 &nolv = (2,3) if $_;
187 1;
188EOE
189
cb949c37 190like($_, qr/Can\'t modify non-lvalue subroutine call in scalar assignment/);
cd06dffe
GS
191
192$x0 = $x1 = $_ = undef;
193$nolv = \&nolv;
194
195eval <<'EOE' or $_ = $@;
196 $nolv->() = (2,3) if $_;
197 1;
198EOE
199
cb949c37 200ok(!defined $_) or diag "'$_', '$x0', '$x1'";
cd06dffe
GS
201
202$x0 = $x1 = $_ = undef;
203$nolv = \&nolv;
204
205eval <<'EOE' or $_ = $@;
206 $nolv->() = (2,3);
207 1;
208EOE
209
cb949c37
NC
210like($_, qr/Can\'t modify non-lvalue subroutine call/)
211 or diag "'$_', '$x0', '$x1'";
cd06dffe 212
e2316ad3 213sub lv0 : lvalue { }
d25b0d7b 214sub rlv0 : lvalue { return }
cd06dffe
GS
215
216$_ = undef;
217eval <<'EOE' or $_ = $@;
218 lv0 = (2,3);
219 1;
220EOE
221
c73030b8 222like($_, qr/Can't return undef from lvalue subroutine/);
cd06dffe 223
cd06dffe
GS
224$_ = undef;
225eval <<'EOE' or $_ = $@;
d25b0d7b
FC
226 rlv0 = (2,3);
227 1;
228EOE
229
230like($_, qr/Can't return undef from lvalue subroutine/,
231 'explicit return of nothing in scalar context');
232
233$_ = undef;
234eval <<'EOE' or $_ = $@;
cd06dffe
GS
235 (lv0) = (2,3);
236 1;
237EOE
238
cb949c37 239ok(!defined $_) or diag $_;
cd06dffe 240
d25b0d7b
FC
241$_ = undef;
242eval <<'EOE' or $_ = $@;
243 (rlv0) = (2,3);
244 1;
245EOE
246
247ok(!defined $_, 'explicit return of nothing in list context') or diag $_;
248
69b22cd1
FC
249($a,$b)=();
250(lv0($a,$b)) = (3,4);
251is +($a//'undef') . ($b//'undef'), 'undefundef',
252 'list assignment to empty lvalue sub';
253
254
a98df962 255sub lv1u :lvalue { undef }
d25b0d7b 256sub rlv1u :lvalue { undef }
cd06dffe
GS
257
258$_ = undef;
259eval <<'EOE' or $_ = $@;
260 lv1u = (2,3);
261 1;
262EOE
263
c73030b8 264like($_, qr/Can't return undef from lvalue subroutine/);
cd06dffe
GS
265
266$_ = undef;
267eval <<'EOE' or $_ = $@;
d25b0d7b
FC
268 rlv1u = (2,3);
269 1;
270EOE
271
272like($_, qr/Can't return undef from lvalue subroutine/,
273 'explicitly returning undef in scalar context');
274
275$_ = undef;
276eval <<'EOE' or $_ = $@;
cd06dffe
GS
277 (lv1u) = (2,3);
278 1;
279EOE
280
730fb7e7
FC
281ok(!defined, 'implicitly returning undef in list context');
282
283$_ = undef;
284eval <<'EOE' or $_ = $@;
285 (rlv1u) = (2,3);
286 1;
287EOE
288
289ok(!defined, 'explicitly returning undef in list context');
cd06dffe
GS
290
291$x = '1234567';
cd06dffe
GS
292
293$_ = undef;
294eval <<'EOE' or $_ = $@;
78f9721b 295 sub lv1t : lvalue { index $x, 2 }
cd06dffe
GS
296 lv1t = (2,3);
297 1;
298EOE
299
145b2bbb 300like($_, qr/Can\'t return a temporary from lvalue subroutine/);
cd06dffe
GS
301
302$_ = undef;
d25b0d7b
FC
303eval <<'EOE' or $_ = $@;
304 sub rlv1t : lvalue { index $x, 2 }
305 rlv1t = (2,3);
306 1;
307EOE
308
309like($_, qr/Can\'t return a temporary from lvalue subroutine/,
310 'returning a PADTMP explicitly');
311
312$_ = undef;
313eval <<'EOE' or $_ = $@;
314 (rlv1t) = (2,3);
315 1;
316EOE
317
318like($_, qr/Can\'t return a temporary from lvalue subroutine/,
319 'returning a PADTMP explicitly (list context)');
320
321$_ = undef;
145b2bbb
FC
322sub lv2t : lvalue { shift }
323(lv2t($_)) = (2,3);
324is($_, 2);
cd06dffe
GS
325
326$xxx = 'xxx';
327sub xxx () { $xxx } # Not lvalue
cd06dffe
GS
328
329$_ = undef;
330eval <<'EOE' or $_ = $@;
78f9721b 331 sub lv1tmp : lvalue { xxx } # is it a TEMP?
cd06dffe
GS
332 lv1tmp = (2,3);
333 1;
334EOE
335
da1dff94 336like($_, qr/Can\'t modify non-lvalue subroutine call at /);
cd06dffe
GS
337
338$_ = undef;
339eval <<'EOE' or $_ = $@;
340 (lv1tmp) = (2,3);
341 1;
342EOE
343
da1dff94 344like($_, qr/Can\'t modify non-lvalue subroutine call at /);
fd6c41ce 345
9a049f1c 346sub yyy () { 'yyy' } # Const, not lvalue
cd06dffe
GS
347
348$_ = undef;
349eval <<'EOE' or $_ = $@;
78f9721b 350 sub lv1tmpr : lvalue { yyy } # is it read-only?
cd06dffe
GS
351 lv1tmpr = (2,3);
352 1;
353EOE
354
145b2bbb 355like($_, qr/Can\'t return a readonly value from lvalue subroutine at/);
cd06dffe
GS
356
357$_ = undef;
358eval <<'EOE' or $_ = $@;
359 (lv1tmpr) = (2,3);
360 1;
361EOE
362
cb949c37 363like($_, qr/Can\'t return a readonly value from lvalue subroutine/);
cd06dffe 364
a98df962 365sub lva : lvalue {@a}
cd06dffe
GS
366
367$_ = undef;
368@a = ();
369$a[1] = 12;
370eval <<'EOE' or $_ = $@;
371 (lva) = (2,3);
372 1;
373EOE
374
cb949c37 375is("'@a' $_", "'2 3' ");
cd06dffe
GS
376
377$_ = undef;
378@a = ();
379$a[0] = undef;
380$a[1] = 12;
381eval <<'EOE' or $_ = $@;
382 (lva) = (2,3);
383 1;
384EOE
385
cb949c37 386is("'@a' $_", "'2 3' ");
cd06dffe 387
40c94d11
FC
388is lva->${\sub { return $_[0] }}, 2,
389 'lvalue->$thing when lvalue returns array';
390
391my @my = qw/ a b c /;
392sub lvmya : lvalue { @my }
393
394is lvmya->${\sub { return $_[0] }}, 3,
395 'lvalue->$thing when lvalue returns lexical array';
396
a98df962 397sub lv1n : lvalue { $newvar }
cd06dffe
GS
398
399$_ = undef;
400eval <<'EOE' or $_ = $@;
401 lv1n = (3,4);
402 1;
403EOE
404
cb949c37 405is("'$newvar' $_", "'4' ");
cd06dffe 406
a98df962 407sub lv1nn : lvalue { $nnewvar }
cd06dffe
GS
408
409$_ = undef;
410eval <<'EOE' or $_ = $@;
411 (lv1nn) = (3,4);
412 1;
413EOE
414
cb949c37 415is("'$nnewvar' $_", "'3' ");
cd06dffe
GS
416
417$a = \&lv1nn;
418$a->() = 8;
cb949c37 419is($nnewvar, '8');
d32f2495 420
84251760 421eval 'sub AUTOLOAD : lvalue { $newvar }';
d32f2495 422foobar() = 12;
cb949c37 423is($newvar, "12");
26191e78 424
da1dff94
FC
425# But autoloading should only be triggered by a call to an undefined
426# subroutine.
427&{"lv1nn"} = 14;
428is $newvar, 12, 'AUTOLOAD does not take precedence over lvalue sub';
429eval { &{"xxx"} = 14 };
430is $newvar, 12, 'AUTOLOAD does not take precedence over non-lvalue sub';
431
78f9721b
SM
432{
433my %hash; my @array;
434sub alv : lvalue { $array[1] }
435sub alv2 : lvalue { $array[$_[0]] }
436sub hlv : lvalue { $hash{"foo"} }
437sub hlv2 : lvalue { $hash{$_[0]} }
438$array[1] = "not ok 51\n";
439alv() = "ok 50\n";
cb949c37 440is(alv(), "ok 50\n");
78f9721b
SM
441
442alv2(20) = "ok 51\n";
cb949c37 443is($array[20], "ok 51\n");
78f9721b
SM
444
445$hash{"foo"} = "not ok 52\n";
446hlv() = "ok 52\n";
cb949c37 447is($hash{foo}, "ok 52\n");
78f9721b
SM
448
449$hash{bar} = "not ok 53\n";
450hlv("bar") = "ok 53\n";
cb949c37 451is(hlv("bar"), "ok 53\n");
78f9721b
SM
452
453sub array : lvalue { @array }
454sub array2 : lvalue { @array2 } # This is a global.
455sub hash : lvalue { %hash }
456sub hash2 : lvalue { %hash2 } # So's this.
457@array2 = qw(foo bar);
458%hash2 = qw(foo bar);
459
460(array()) = qw(ok 54);
cb949c37 461is("@array", "ok 54");
78f9721b
SM
462
463(array2()) = qw(ok 55);
cb949c37 464is("@array2", "ok 55");
78f9721b
SM
465
466(hash()) = qw(ok 56);
cb949c37 467cmp_ok($hash{ok}, '==', 56);
78f9721b
SM
468
469(hash2()) = qw(ok 57);
cb949c37 470cmp_ok($hash2{ok}, '==', 57);
78f9721b
SM
471
472@array = qw(a b c d);
473sub aslice1 : lvalue { @array[0,2] };
474(aslice1()) = ("ok", "already");
cb949c37 475is("@array", "ok b already d");
78f9721b
SM
476
477@array2 = qw(a B c d);
478sub aslice2 : lvalue { @array2[0,2] };
479(aslice2()) = ("ok", "already");
cb949c37 480is("@array2", "ok B already d");
78f9721b
SM
481
482%hash = qw(a Alpha b Beta c Gamma);
483sub hslice : lvalue { @hash{"c", "b"} }
484(hslice()) = ("CISC", "BogoMIPS");
cb949c37 485is(join("/",@hash{"c","a","b"}), "CISC/Alpha/BogoMIPS");
78f9721b
SM
486}
487
488$str = "Hello, world!";
489sub sstr : lvalue { substr($str, 1, 4) }
490sstr() = "i";
cb949c37 491is($str, "Hi, world!");
78f9721b
SM
492
493$str = "Made w/ JavaScript";
494sub veclv : lvalue { vec($str, 2, 32) }
e6b8b224
PP
495if (ord('A') != 193) {
496 veclv() = 0x5065726C;
497}
498else { # EBCDIC?
499 veclv() = 0xD7859993;
500}
cb949c37 501is($str, "Made w/ PerlScript");
78f9721b
SM
502
503sub position : lvalue { pos }
504@p = ();
505$_ = "fee fi fo fum";
506while (/f/g) {
507 push @p, position;
508 position() += 6;
509}
cb949c37 510is("@p", "1 8");
7c8af4ef 511
fad4a2e4
FC
512sub keeze : lvalue { keys %__ }
513%__ = ("a","b");
514keeze = 64;
515is scalar %__, '1/64', 'keys assignment through lvalue sub';
516
7c8af4ef
RG
517# Bug 20001223.002: split thought that the list had only one element
518@ary = qw(4 5 6);
519sub lval1 : lvalue { $ary[0]; }
520sub lval2 : lvalue { $ary[1]; }
521(lval1(), lval2()) = split ' ', "1 2 3 4";
cb949c37
NC
522
523is(join(':', @ary), "1:2:6");
1c4274f4 524
f9bc45ef
TP
525# check that an element of a tied hash/array can be assigned to via lvalueness
526
527package Tie_Hash;
528
529our ($key, $val);
530sub TIEHASH { bless \my $v => __PACKAGE__ }
531sub STORE { ($key, $val) = @_[1,2] }
532
533package main;
534sub lval_tie_hash : lvalue {
535 tie my %t => 'Tie_Hash';
536 $t{key};
537}
538
539eval { lval_tie_hash() = "value"; };
540
cb949c37 541is($@, "", "element of tied hash");
f9bc45ef 542
cb949c37 543is("$Tie_Hash::key-$Tie_Hash::val", "key-value");
f9bc45ef
TP
544
545
546package Tie_Array;
547
548our @val;
549sub TIEARRAY { bless \my $v => __PACKAGE__ }
550sub STORE { $val[ $_[1] ] = $_[2] }
551
552package main;
553sub lval_tie_array : lvalue {
554 tie my @t => 'Tie_Array';
555 $t[0];
556}
557
558eval { lval_tie_array() = "value"; };
559
f9bc45ef 560
cb949c37 561is($@, "", "element of tied array");
f9bc45ef 562
cb949c37 563is ($Tie_Array::val[0], "value");
1c4274f4 564
1c4274f4 565
4bee03f8
FC
566# Check that tied pad vars that are returned can be assigned to
567sub TIESCALAR { bless [] }
568sub STORE {$wheel = $_[1]}
569sub FETCH {$wheel}
570sub tied_pad_var :lvalue { tie my $tyre, ''; $tyre }
571sub tied_pad_varr :lvalue { tie my $tyre, ''; return $tyre }
572tied_pad_var = 1;
573is $wheel, 1, 'tied pad var returned in scalar lvalue context';
574tied_pad_var->${\sub{ $_[0] = 2 }};
575is $wheel, 2, 'tied pad var returned in scalar ref context';
576(tied_pad_var) = 3;
577is $wheel, 3, 'tied pad var returned in list lvalue context';
578$_ = 4 for tied_pad_var;
579is $wheel, 4, 'tied pad var returned in list ref context';
580tied_pad_varr = 5;
581is $wheel, 5, 'tied pad var explicitly returned in scalar lvalue context';
582tied_pad_varr->${\sub{ $_[0] = 6 }};
583is $wheel, 6, 'tied pad var explicitly returned in scalar ref context';
584(tied_pad_varr) = 7;
585is $wheel, 7, 'tied pad var explicitly returned in list lvalue context';
586$_ = 8 for tied_pad_varr;
587is $wheel, 8, 'tied pad var explicitly returned in list ref context';
588
589
fa1e92c4
FC
590# Test explicit return of lvalue expression
591{
592 # subs are copies from tests 1-~18 with an explicit return added.
593 # They used not to work, which is why they are ‘badly’ named.
1c4274f4
MS
594 sub bad_get_lex : lvalue { return $in };
595 sub bad_get_st : lvalue { return $blah }
596
597 sub bad_id : lvalue { return ${\shift} }
598 sub bad_id1 : lvalue { return $_[0] }
599 sub bad_inc : lvalue { return ${\++$_[0]} }
600
601 $in = 5;
602 $blah = 3;
603
604 bad_get_st = 7;
605
606 is( $blah, 7 );
607
608 bad_get_lex = 7;
609
610 is($in, 7, "yada");
611
612 ++bad_get_st;
613
614 is($blah, 8, "yada");
07fd1c9c
FC
615
616 ++bad_get_lex;
617 cmp_ok($in, '==', 8);
618
619 bad_id(bad_get_st) = 10;
620 cmp_ok($blah, '==', 10);
621
622 bad_id(bad_get_lex) = 10;
623 cmp_ok($in, '==', 10);
624
625 ++bad_id(bad_get_st);
626 cmp_ok($blah, '==', 11);
627
628 ++bad_id(bad_get_lex);
629 cmp_ok($in, '==', 11);
630
631 bad_id1(bad_get_st) = 20;
632 cmp_ok($blah, '==', 20);
633
634 bad_id1(bad_get_lex) = 20;
635 cmp_ok($in, '==', 20);
636
637 ++bad_id1(bad_get_st);
638 cmp_ok($blah, '==', 21);
639
640 ++bad_id1(bad_get_lex);
641 cmp_ok($in, '==', 21);
642
643 bad_inc(bad_get_st);
644 cmp_ok($blah, '==', 22);
645
646 bad_inc(bad_get_lex);
647 cmp_ok($in, '==', 22);
648
649 bad_inc(bad_id(bad_get_st));
650 cmp_ok($blah, '==', 23);
651
652 bad_inc(bad_id(bad_get_lex));
653 cmp_ok($in, '==', 23);
654
655 ++bad_inc(bad_id1(bad_id(bad_get_st)));
656 cmp_ok($blah, '==', 25);
657
658 ++bad_inc(bad_id1(bad_id(bad_get_lex)));
659 cmp_ok($in, '==', 25);
1ffdc07c
FC
660
661 # Recursive
662 my $r;
663 my $to_modify;
664 $r = sub :lvalue {
665 my $depth = shift//0;
666 if ($depth == 2) { return $to_modify }
667 return &$r($depth+1);
668 };
669 &$r(0) = 7;
670 is $to_modify, 7, 'recursive lvalue sub';
f6a9f8a4
FC
671
672 # Recursive with substr [perl #72706]
673 my $val = '';
674 my $pie;
675 $pie = sub :lvalue {
676 my $depth = shift;
677 return &$pie($depth) if $depth--;
678 substr $val, 0;
679 };
680 for my $depth (0, 1, 2) {
681 my $value = "Good $depth";
682 eval {
683 &$pie($depth) = $value;
684 };
685 is($@, '', "recursive lvalue substr return depth $depth");
686 is($val, $value,
687 "value assigned to recursive lvalue substr (depth $depth)");
688 }
1c4274f4
MS
689}
690
91e34d82 691{ # bug #23790
4546bcba
RGS
692 my @arr = qw /one two three/;
693 my $line = "zero";
694 sub lval_array () : lvalue {@arr}
695
696 for (lval_array) {
697 $line .= $_;
698 }
699
700 is($line, "zeroonetwothree");
91e34d82
MP
701
702 sub trythislval { scalar(@_)."x".join "", @_ }
703 is(trythislval(lval_array()), "3xonetwothree");
704
705 sub changeme { $_[2] = "free" }
706 changeme(lval_array);
707 is("@arr", "one two free");
40c94d11
FC
708
709 # test again, with explicit return
710 sub rlval_array() : lvalue {return @arr}
711 @arr = qw /one two three/;
712 $line = "zero";
713 for (rlval_array) {
714 $line .= $_;
715 }
716 is($line, "zeroonetwothree");
717 is(trythislval(rlval_array()), "3xonetwothree");
718 changeme(rlval_array);
719 is("@arr", "one two free");
720
721 # Variations on the same theme, with multiple vars returned
722 my $scalar = 'half';
723 sub lval_scalar_array () : lvalue { $scalar, @arr }
724 @arr = qw /one two three/;
725 $line = "zero";
726 for (lval_scalar_array) {
727 $line .= $_;
728 }
729 is($line, "zerohalfonetwothree");
730 is(trythislval(lval_scalar_array()), "4xhalfonetwothree");
731 changeme(lval_scalar_array);
732 is("@arr", "one free three");
733
734 sub lval_array_scalar () : lvalue { @arr, $scalar }
735 @arr = qw /one two three/;
736 $line = "zero";
737 $scalar = 'four';
738 for (lval_array_scalar) {
739 $line .= $_;
740 }
741 is($line, "zeroonetwothreefour");
742 is(trythislval(lval_array_scalar()), "4xonetwothreefour");
743 changeme(lval_array_scalar);
744 is("@arr", "one two free");
745
746 # Tests for specific ops not tested above
747 # rv2av
748 @array2 = qw 'one two free';
749 is join(',', map $_, sub:lvalue{@array2}->()), 'one,two,free',
750 'rv2av in reference context';
751 is join(',', map $_, sub:lvalue{@{\@array2}}->()), 'one,two,free',
752 'rv2av-with-ref in reference context';
753 # padhv
754 my %hash = qw[a b c d];
755 like join(',', map $_, sub:lvalue{%hash}->()),
756 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'padhv in reference context';
757 # rv2hv
758 %hash2 = qw[a b c d];
759 like join(',', map $_, sub:lvalue{%hash2}->()),
760 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'rv2hv in reference context';
761 like join(',', map $_, sub:lvalue{%{\%hash2}}->()),
762 qr/^(?:a,b,c,d|c,d,a,b)\z/, 'rv2hv-with-ref in reference context';
4546bcba 763}
cb0d96b9
NC
764
765{
766 package Foo;
767 sub AUTOLOAD :lvalue { *{$AUTOLOAD} };
768 package main;
769 my $foo = bless {},"Foo";
770 my $result;
771 $foo->bar = sub { $result = "bar" };
772 $foo->bar;
773 is ($result, 'bar', "RT #41550");
774}
885ef6f5 775
c70e3f2a 776SKIP: { skip 'no attributes.pm', 1 unless eval 'require attributes';
885ef6f5
GG
777fresh_perl_is(<<'----', <<'====', "lvalue can not be set after definition. [perl #68758]");
778use warnings;
779our $x;
780sub foo { $x }
781sub foo : lvalue;
fff96ff7
FC
782sub MODIFY_CODE_ATTRIBUTES {}
783sub foo : lvalue : fr0g;
885ef6f5
GG
784foo = 3;
785----
786lvalue attribute ignored after the subroutine has been defined at - line 4.
fff96ff7
FC
787lvalue attribute ignored after the subroutine has been defined at - line 6.
788Can't modify non-lvalue subroutine call in scalar assignment at - line 7, near "3;"
885ef6f5
GG
789Execution of - aborted due to compilation errors.
790====
c70e3f2a 791}
eac910c8
GG
792
793{
794 my $x;
795 sub lval_decl : lvalue;
796 sub lval_decl { $x }
797 lval_decl = 5;
798 is($x, 5, "subroutine declared with lvalue before definition retains lvalue. [perl #68758]");
799}
f71f472f 800
c70e3f2a 801SKIP: { skip "no attributes.pm", 2 unless eval { require attributes };
b4c6bb84
FC
802 sub utf8::valid :lvalue;
803 require attributes;
804 is "@{[ &attributes::get(\&utf8::valid) ]}", 'lvalue',
4dbb339a
FC
805 'sub declaration with :lvalue applies it to XSUBs';
806
b4c6bb84
FC
807 BEGIN { *wonky = \&marjibberous }
808 sub wonky :lvalue;
809 is "@{[ &attributes::get(\&wonky) ]}", 'lvalue',
4dbb339a 810 'sub declaration with :lvalue applies it to assigned stub';
c70e3f2a 811}
4dbb339a 812
f71f472f
FC
813sub fleen : lvalue { $pnare }
814$pnare = __PACKAGE__;
815ok eval { fleen = 1 }, "lvalues can return COWs (CATTLE?) [perl #75656]";\
816is $pnare, 1, 'and returning CATTLE actually works';
a0aa6076
FC
817$pnare = __PACKAGE__;
818ok eval { (fleen) = 1 }, "lvalues can return COWs in list context";
819is $pnare, 1, 'and returning COWs in list context actually works';
a1302723
FC
820$pnare = __PACKAGE__;
821ok eval { $_ = 1 for(fleen); 1 }, "lvalues can return COWs in ref cx";
822is $pnare, 1, 'and returning COWs in reference context actually works';
145b2bbb
FC
823
824
825# Returning an arbitrary expression, not necessarily lvalue
826+sub :lvalue { return $ambaga || $ambaga }->() = 73;
827is $ambaga, 73, 'explicit return of arbitrary expression (scalar context)';
828(sub :lvalue { return $ambaga || $ambaga }->()) = 74;
829is $ambaga, 74, 'explicit return of arbitrary expression (list context)';
830+sub :lvalue { $ambaga || $ambaga }->() = 73;
831is $ambaga, 73, 'implicit return of arbitrary expression (scalar context)';
832(sub :lvalue { $ambaga || $ambaga }->()) = 74;
833is $ambaga, 74, 'implicit return of arbitrary expression (list context)';
145b2bbb
FC
834eval { +sub :lvalue { return 3 }->() = 4 };
835like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
836 'assignment to numeric constant explicitly returned from lv sub';
837eval { (sub :lvalue { return 3 }->()) = 4 };
838like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
839 'assignment to num constant explicitly returned (list cx)';
145b2bbb
FC
840eval { +sub :lvalue { 3 }->() = 4 };
841like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
842 'assignment to numeric constant implicitly returned from lv sub';
843eval { (sub :lvalue { 3 }->()) = 4 };
844like $@, qr/Can\'t return a readonly value from lvalue subroutine at/,
845 'assignment to num constant implicitly returned (list cx)';
bf8fb5eb
FC
846
847# reference (potential lvalue) context
848$suffix = '';
849for my $sub (sub :lvalue {$_}, sub :lvalue {return $_}) {
850 &$sub()->${\sub { $_[0] = 37 }};
851 is $_, '37', 'lvalue->method'.$suffix;
852 ${\scalar &$sub()} = 38;
853 is $_, '38', 'scalar(lvalue)'.$suffix;
854 sub assign39_with_proto ($) { $_[0] = 39 }
855 assign39_with_proto(&$sub());
856 is $_, '39', 'func(lvalue) when func has $ proto'.$suffix;
857 $_ = 1;
858 ${\(&$sub()||undef)} = 40;
859 is $_, '40', 'lvalue||...'.$suffix;
860 ${\(${\undef}||&$sub())} = 41; # extra ${\...} to bypass const folding
861 is $_, '41', '...||lvalue'.$suffix;
862 $_ = 0;
863 ${\(&$sub()&&undef)} = 42;
864 is $_, '42', 'lvalue&&...'.$suffix;
865 ${\(${\1}&&&$sub())} = 43;
866 is $_, '43', '...&&lvalue'.$suffix;
867 ${\(&$sub())[0]} = 44;
868 is $_, '44', '(lvalue)[0]'.$suffix;
869}
870continue { $suffix = ' (explicit return)' }
767eda44
FC
871
872# autovivification
d507ecb9 873$suffix = '';
767eda44
FC
874for my $sub (sub :lvalue {$_}, sub :lvalue {return $_}) {
875 undef $_;
876 &$sub()->[3] = 4;
877 is $_->[3], 4, 'func->[...] autovivification'.$suffix;
878 undef $_;
879 &$sub()->{3} = 4;
880 is $_->{3}, 4, 'func->{...} autovivification'.$suffix;
881 undef $_;
882 ${&$sub()} = 4;
883 is $$_, 4, '${func()} autovivification' .$suffix;
884 undef $_;
885 @{&$sub()} = 4;
886 is "@$_", 4, '@{func()} autovivification' .$suffix;
887 undef $_;
888 %{&$sub()} = (4,5);
889 is join('-',%$_), '4-5', '%{func()} autovivification'.$suffix;
0e9700df
GG
890 undef $_;
891 ${ (), &$sub()} = 4;
892 is $$_, 4, '${ (), func()} autovivification' .$suffix;
767eda44
FC
893}
894continue { $suffix = ' (explicit return)' }
e08be60b
FC
895
896# [perl #92406] [perl #92290] Returning a pad var in rvalue context
897$suffix = '';
898for my $sub (
899 sub :lvalue { my $x = 72; $x },
900 sub :lvalue { my $x = 72; return $x }
901) {
902 is scalar(&$sub), 72, "sub returning pad var in scalar context$suffix";
903 is +(&$sub)[0], 72, "sub returning pad var in list context$suffix";
904}
905continue { $suffix = ' (explicit return)' }
ad37a74e
FC
906
907# Returning read-only values in reference context
908$suffix = '';
909for (
910 sub :lvalue { $] }->(),
911 sub :lvalue { return $] }->()
912) {
913 is \$_, \$], 'read-only values are returned in reference context'
914 .$suffix # (they used to be copied)
915}
916continue { $suffix = ' (explicit return)' }
777d9014
FC
917
918# Returning unwritables from nested lvalue sub call in in rvalue context
919# First, ensure we are testing what we think we are:
920if (!Internals::SvREADONLY($])) { Internals::SvREADONLY($],1); }
921sub squibble : lvalue { return $] }
922sub squebble : lvalue { squibble }
923sub squabble : lvalue { return squibble }
924is $x = squebble, $], 'returning ro from nested lv sub call in rv cx';
925is $x = squabble, $], 'explct. returning ro from nested lv sub in rv cx';
926is \squebble, \$], 'returning ro from nested lv sub call in ref cx';
927is \squabble, \$], 'explct. returning ro from nested lv sub in ref cx';
da1dff94
FC
928
929# [perl #102486] Sub calls as the last statement of an lvalue sub
930package _102486 {
931 my $called;
932 my $x = 'nonlv';
933 sub strictlv :lvalue { use strict 'refs'; &$x }
934 sub lv :lvalue { &$x }
935 sub nonlv { ++$called }
936 eval { strictlv };
937 ::like $@, qr/^Can't use string \("nonlv"\) as a subroutine ref while/,
938 'strict mode applies to sub:lvalue{ &$string }';
939 $called = 0;
940 ::ok eval { lv },
941 'sub:lvalue{&$x}->() does not die for non-lvalue inner sub call';
942 ::is $called, 1, 'The &$x actually called the sub';
943 eval { +sub :lvalue { &$x }->() = 3 };
944 ::like $@, qr/^Can't modify non-lvalue subroutine call at /,
945 'sub:lvalue{&$x}->() dies in true lvalue context';
946}
5811c07e
FC
947
948# TARG should be copied in rvalue context
949sub ucf :lvalue { ucfirst $_[0] }
950is ucf("just another ") . ucf("perl hacker,\n"),
951 "Just another Perl hacker,\n", 'TARG is copied in rvalue scalar cx';
952is join('',ucf("just another "), ucf "perl hacker,\n"),
953 "Just another Perl hacker,\n", 'TARG is copied in rvalue list cx';
954sub ucfr : lvalue {
955 @_ ? ucfirst $_[0] : do {
956 is ucfr("just another ") . ucfr("perl hacker,\n"),
957 "Just another Perl hacker,\n",
958 'TARG is copied in recursive rvalue scalar cx';
959 is join('',ucfr("just another "), ucfr("perl hacker,\n")),
960 "Just another Perl hacker,\n",
961 'TARG is copied in recursive rvalue list cx';
962 }
963}
964ucfr();