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