summaryrefslogtreecommitdiff
path: root/t/op/lvref.t
blob: ef1a6c2b2753da8b4e633469146e929a5957ddd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
BEGIN {
    chdir 't';
    require './test.pl';
    set_up_inc("../lib");
}

plan 42;

sub on { $::TODO = ' ' }
sub off{ $::TODO = ''  }

eval '\$x = \$y';
like $@, qr/^Experimental lvalue references not enabled/,
    'error when feature is disabled';
eval '\($x) = \$y';
like $@, qr/^Experimental lvalue references not enabled/,
    'error when feature is disabled (aassign)';

use feature 'lvalue_refs';

{
    my($w,$c);
    local $SIG{__WARN__} = sub { $c++; $w = shift };
    eval '\$x = \$y';
    is $c, 1, 'one warning from lv ref assignment';
    like $w, qr/^Lvalue references are experimental/,
        'experimental warning';
    undef $c;
    eval '\($x) = \$y';
    is $c, 1, 'one warning from lv ref list assignment';
    like $w, qr/^Lvalue references are experimental/,
        'experimental warning';
}

no warnings 'experimental::lvalue_refs';

# Scalars

eval '\$x = \$y';
is \$x, \$y, '\$pkg_scalar = ...';
my $m;
\$m = \$y;
is \$m, \$y, '\$lexical = ...';
\my $n = \$y;
is \$n, \$y, '\my $lexical = ...';
@_ = \$_;
\($x) = @_;
is \$x, \$_, '\($pkgvar) = ... gives list context';
undef *x;
(\$x) = @_;
is \$x, \$_, '(\$pkgvar) = ... gives list context';
on;
my $o;
eval '\($o) = @_';
is \$o, \$_, '\($lexical) = ... gives list cx';
my $q;
eval '(\$q) = @_';
is \$q, \$_, '(\$lexical) = ... gives list cx';
eval '\(my $p) = @_';
is \$p, \$_, '\(my $lexical) = ... gives list cx';
eval '(\my $r) = @_';
is \$r, \$_, '(\my $lexical) = ... gives list cx';
eval '\my($s) = @_';
is \$s, \$_, '\my($lexical) = ... gives list cx';
eval '\($_a, my $a) = @{[\$b, \$c]}';
is \$_a, \$b, 'package scalar in \(...)';
is \$a, \$c, 'lex scalar in \(...)';
eval '(\$_b, \my $b) = @{[\$b, \$c]}';
is \$_b, \$::b, 'package scalar in (\$foo, \$bar)';
is \$b, \$c, 'lex scalar in (\$foo, \$bar)';
is eval '\local $l = \3; $l', 3, '\local $scalar assignment';
off;
is $l, undef, 'localisation unwound';
\$foo = \*bar;
is *foo{SCALAR}, *bar{GLOB}, 'globref-to-scalarref assignment';
on;

# Array Elements

# ...

# Hash Elements

# ...

# Arrays

# ...

# Hashes

# ...

# Subroutines

# ...

# Mixed List Assignments

off;
(\$tahi, $rua) = \(1,2);
is join(' ', $tahi, $$rua), '1 2',
  'mixed scalar ref and scalar list assignment';
on;

# Conditional expressions

$_ = 3;
eval '$_ == 3 ? \$tahi : $rua = \3';
is $tahi, 3, 'cond assignment resolving to scalar ref';
eval '$_ == 3 ? \$toru : $wha = \3';
is $$wha, 3, 'cond assignment resolving to scalar';
eval '$_ == 3 ? \$rima : \$ono = \5';
is $$rima, 5, 'cond assignment with refgens on both branches';

# Foreach

eval '
  for \my $a(\$for1, \$for2) {
    push @for, \$a;
  }
';
is "@for", \$for1 . ' ' . \$for2, 'foreach \my $a';

@for = ();
eval '
  for \my @a([1,2], [3,4]) {
    push @for, @a;
  }
';
is "@for", "1 2 3 4", 'foreach \my @a [perl #22335]';

@for = ();
eval '
  for \my %a({5,6}, {7,8}) {
    push @for, %a;
  }
';
is "@for", "5 6 7 8", 'foreach \my %a [perl #22335]';

@for = ();
eval '
  for \my &a(sub {9}, sub {10}) {
    push @for, &a;
  }
';
is "@for", "9 10", 'foreach \my &a';


# Errors

off;
eval { my $x; \$x = 3 };
like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
eval { my $x; \$x = [] };
like $@, qr/^Assigned value is not a SCALAR reference at/,
    'assigning non-scalar ref to scalar ref';
eval { \$::x = [] };
like $@, qr/^Assigned value is not a SCALAR reference at/,
    'assigning non-scalar ref to package scalar ref';

on;
eval '(\do{}) = 42';
like $@, qr/^Can't modify reference to do block in list assignment at /,
    "Can't modify reference to do block in list assignment";
off;
eval '(\pos) = 42';
like $@,
     qr/^Can't modify reference to match position in list assignment at /,
    "Can't modify ref to some scalar-returning op in list assignment";
on;
eval '(\glob) = 42';
like $@,
     qr/^Can't modify reference to glob in list assignment at /,
    "Can't modify reference to some list-returning op in list assignment";
off;
eval '\pos = 42';
like $@,
    qr/^Can't modify reference to match position in scalar assignment at /,
   "Can't modify ref to some scalar-returning op in scalar assignment";
on;

# Miscellaneous

{
  my($x,$y);
  sub {
    sub {
      \$x = \$y;
    }->();
    is \$x, \$y, 'lexical alias affects outer closure';
  }->();
  is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
}

{ # PADSTALE has a double meaning
  use feature 'lexical_subs', 'signatures', 'state';
  no warnings 'experimental';
  my $c;
  my sub s ($arg) {
    state $x = ++$c;
    if ($arg == 3) { return $c }
    goto skip if $arg == 2;
    my $y;
   skip:
    # $y is PADSTALE the 2nd time
    \$x = \$y if $arg == 2;
  }
  s(1);
  s(2);
  is s(3), 1, 'padstale alias should not reset state'
}

off;
SKIP: {
    skip_without_dynamic_extension('List/Util');
    require Scalar::Util;
    my $a;
    Scalar::Util::weaken($r = \$a);
    \$a = $r;
    pass 'no crash when assigning \$lex = $weakref_to_lex'
}