This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lvref.t: Some assignments with mixed lhs
[perl5.git] / t / op / lvref.t
CommitLineData
72ed4618
FC
1BEGIN {
2 chdir 't';
3 require './test.pl';
4 set_up_inc("../lib");
5}
6
9fc71ff4 7plan 30;
72ed4618 8
53abf431
FC
9sub on { $::TODO = ' ' }
10sub off{ $::TODO = '' }
72ed4618
FC
11
12eval '\$x = \$y';
13like $@, qr/^Experimental lvalue references not enabled/,
14 'error when feature is disabled';
15
16use feature 'lvalue_refs';
17
18{
19 my($w,$c);
20 local $SIG{__WARN__} = sub { $c++; $w = shift };
21 eval '\$x = \$y';
22 is $c, 1, 'one warning from lv ref assignment';
23 like $w, qr/^Lvalue references are experimental/,
24 'experimental warning';
25}
26
27no warnings 'experimental::lvalue_refs';
28
29# Scalars
30
31eval '\$x = \$y';
32is \$x, \$y, '\$pkg_scalar = ...';
33my $m;
53abf431 34\$m = \$y;
72ed4618 35is \$m, \$y, '\$lexical = ...';
fc048fcf 36\my $n = \$y;
72ed4618 37is \$n, \$y, '\my $lexical = ...';
fc048fcf 38on;
72ed4618
FC
39@_ = \$_;
40eval '\($x) = @_';
41is \$x, \$_, '\($pkgvar) = ... gives list context';
42my $o;
43eval '\($o) = @_';
44is \$o, \$_, '\($lexical) = ... gives list cx';
45eval '\(my $p) = @_';
46is \$p, \$_, '\(my $lexical) = ... gives list cx';
47eval '\($_a, my $a) = @{[\$b, \$c]}';
48is \$_a, \$b, 'package scalar in \(...)';
49is \$a, \$c, 'lex scalar in \(...)';
50eval '(\$_b, \my $b) = @{[\$b, \$c]}';
51is \$_b, \$::b, 'package scalar in (\$foo, \$bar)';
52is \$b, \$c, 'lex scalar in (\$foo, \$bar)';
11ea28ee
FC
53is eval '\local $l = \3; $l', 3, '\local $scalar assignment';
54off;
55is $l, undef, 'localisation unwound';
29a3d628
FC
56\$foo = \*bar;
57is *foo{SCALAR}, *bar{GLOB}, 'globref-to-scalarref assignment';
11ea28ee 58on;
72ed4618
FC
59
60# Array Elements
61
62# ...
63
64# Hash Elements
65
66# ...
67
68# Arrays
69
70# ...
71
72# Hashes
73
74# ...
75
76# Subroutines
77
78# ...
79
9fc71ff4 80# Mixed (List) Assignments
72ed4618 81
9fc71ff4
FC
82eval '(\$tahi, $rua) = \(1,2)';
83is join(' ', $tahi, $$rua), '1 2',
84 'mixed scalar ref and scalar list assignment';
85$_ = 3;
86eval '$_ == 3 ? \$tahi : $rua = \3';
87is $tahi, 3, 'cond assignment resolving to scalar ref';
88eval '$_ == 3 ? \$toru : $wha = \3';
89is $$wha, 3, 'cond assignment resolving to scalar';
72ed4618 90
096cc2cc
FC
91# Foreach
92
93eval '
94 for \my $a(\$for1, \$for2) {
95 push @for, \$a;
96 }
97';
98is "@for", \$for1 . ' ' . \$for2, 'foreach \my $a';
99
100@for = ();
101eval '
102 for \my @a([1,2], [3,4]) {
103 push @for, @a;
104 }
105';
106is "@for", "1 2 3 4", 'foreach \my @a [perl #22335]';
107
108@for = ();
109eval '
110 for \my %a({5,6}, {7,8}) {
111 push @for, %a;
112 }
113';
114is "@for", "5 6 7 8", 'foreach \my %a [perl #22335]';
115
116@for = ();
117eval '
118 for \my &a(sub {9}, sub {10}) {
119 push @for, &a;
120 }
121';
122is "@for", "9 10", 'foreach \my &a';
123
124
72ed4618
FC
125# Errors
126
b3717a0e
FC
127off;
128eval { my $x; \$x = 3 };
129like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
130eval { my $x; \$x = [] };
131like $@, qr/^Assigned value is not a SCALAR reference at/,
132 'assigning non-scalar ref to scalar ref';
d8a875d9
FC
133eval { \$::x = [] };
134like $@, qr/^Assigned value is not a SCALAR reference at/,
135 'assigning non-scalar ref to package scalar ref';
b3717a0e 136on;
781ff25d
FC
137
138# Miscellaneous
139
140{
141 my($x,$y);
142 sub {
143 sub {
144 \$x = \$y;
145 }->();
146 is \$x, \$y, 'lexical alias affects outer closure';
147 }->();
148 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
149}
81cb1af6
FC
150
151{ # PADSTALE has a double meaning
152 use feature 'lexical_subs', 'signatures', 'state';
153 no warnings 'experimental';
154 my $c;
155 my sub s ($arg) {
156 state $x = ++$c;
157 if ($arg == 3) { return $c }
158 goto skip if $arg == 2;
159 my $y;
160 skip:
161 # $y is PADSTALE the 2nd time
162 \$x = \$y if $arg == 2;
163 }
164 s(1);
165 s(2);
166 is s(3), 1, 'padstale alias should not reset state'
167}
cf5d2d91
FC
168
169off;
170SKIP: {
171 skip_without_dynamic_extension('List/Util');
172 require Scalar::Util;
173 my $a;
174 Scalar::Util::weaken($r = \$a);
175 \$a = $r;
176 pass 'no crash when assigning \$lex = $weakref_to_lex'
177}