This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add save_adelete()/SAVEADELETE() to save on the stack an array element delete
[perl5.git] / t / op / local.t
CommitLineData
a687059c
LW
1#!./perl
2
d441d3db
RD
3BEGIN {
4 chdir 't' if -d 't';
28e491ba 5 @INC = qw(. ../lib);
d441d3db
RD
6 require './test.pl';
7}
07a28ea7 8plan tests => 123;
a687059c 9
13414bd5
JM
10my $list_assignment_supported = 1;
11
12#mg.c says list assignment not supported on VMS, EPOC, and SYMBIAN.
13$list_assignment_supported = 0 if ($^O eq 'VMS');
14
15
a687059c
LW
16sub foo {
17 local($a, $b) = @_;
18 local($c, $d);
d441d3db
RD
19 $c = "c 3";
20 $d = "d 4";
21 { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
22 is($a, "a 1");
23 is($b, "b 2");
24 $c, $d;
a687059c
LW
25}
26
d441d3db
RD
27$a = "a 5";
28$b = "b 6";
29$c = "c 7";
30$d = "d 8";
a687059c 31
d441d3db
RD
32my @res;
33@res = &foo("a 1","b 2");
34is($res[0], "c 3");
35is($res[1], "d 4");
a687059c 36
d441d3db
RD
37is($a, "a 5");
38is($b, "b 6");
39is($c, "c 7");
40is($d, "d 8");
41is($x, "a 9");
42is($y, "c 10");
a687059c
LW
43
44# same thing, only with arrays and associative arrays
45
46sub foo2 {
47 local($a, @b) = @_;
48 local(@c, %d);
d441d3db
RD
49 @c = "c 3";
50 $d{''} = "d 4";
51 { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
52 is($a, "a 1");
53 is("@b", "b 2");
54 $c[0], $d{''};
a687059c
LW
55}
56
d441d3db
RD
57$a = "a 5";
58@b = "b 6";
59@c = "c 7";
60$d{''} = "d 8";
61
62@res = &foo2("a 1","b 2");
63is($res[0], "c 3");
64is($res[1], "d 4");
a687059c 65
d441d3db
RD
66is($a, "a 5");
67is("@b", "b 6");
68is($c[0], "c 7");
69is($d{''}, "d 8");
70is($x, "a 19");
71is($y, "c 20");
a687059c 72
706a304b
SM
73
74eval 'local($$e)';
d441d3db 75like($@, qr/Can't localize through a reference/);
706a304b 76
82d03984 77eval '$e = []; local(@$e)';
d441d3db 78like($@, qr/Can't localize through a reference/);
706a304b 79
82d03984 80eval '$e = {}; local(%$e)';
d441d3db 81like($@, qr/Can't localize through a reference/);
85aff577 82
161b7d16
SM
83# Array and hash elements
84
85@a = ('a', 'b', 'c');
86{
87 local($a[1]) = 'foo';
88 local($a[2]) = $a[2];
d441d3db
RD
89 is($a[1], 'foo');
90 is($a[2], 'c');
161b7d16
SM
91 undef @a;
92}
d441d3db
RD
93is($a[1], 'b');
94is($a[2], 'c');
95ok(!defined $a[0]);
161b7d16
SM
96
97@a = ('a', 'b', 'c');
98{
99 local($a[1]) = "X";
100 shift @a;
101}
d441d3db 102is($a[0].$a[1], "Xb");
d60c5a05
RD
103{
104 my $d = "@a";
105 local @a = @a;
106 is("@a", $d);
107}
161b7d16
SM
108
109%h = ('a' => 1, 'b' => 2, 'c' => 3);
110{
111 local($h{'a'}) = 'foo';
112 local($h{'b'}) = $h{'b'};
d441d3db
RD
113 is($h{'a'}, 'foo');
114 is($h{'b'}, 2);
161b7d16
SM
115 local($h{'c'});
116 delete $h{'c'};
117}
d441d3db
RD
118is($h{'a'}, 1);
119is($h{'b'}, 2);
d60c5a05
RD
120{
121 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
122 local %h = %h;
123 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
124}
d441d3db 125is($h{'c'}, 3);
2bb40b7f
GS
126
127# check for scope leakage
128$a = 'outer';
129if (1) { local $a = 'inner' }
d441d3db 130is($a, 'outer');
2bb40b7f
GS
131
132# see if localization works when scope unwinds
133local $m = 5;
134eval {
135 for $m (6) {
136 local $m = 7;
137 die "bye";
138 }
139};
d441d3db 140is($m, 5);
4e4c362e
GS
141
142# see if localization works on tied arrays
143{
144 package TA;
145 sub TIEARRAY { bless [], $_[0] }
146 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
147 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
148 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
149 sub FETCHSIZE { scalar(@{$_[0]}) }
150 sub SHIFT { shift (@{$_[0]}) }
151 sub EXTEND {}
152}
153
154tie @a, 'TA';
155@a = ('a', 'b', 'c');
156{
157 local($a[1]) = 'foo';
be6c24e0 158 local($a[2]) = $a[2];
d441d3db
RD
159 is($a[1], 'foo');
160 is($a[2], 'c');
4e4c362e
GS
161 @a = ();
162}
d441d3db
RD
163is($a[1], 'b');
164is($a[2], 'c');
165ok(!defined $a[0]);
d60c5a05
RD
166{
167 my $d = "@a";
168 local @a = @a;
169 is("@a", $d);
170}
4e4c362e
GS
171
172{
173 package TH;
174 sub TIEHASH { bless {}, $_[0] }
175 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
176 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
c39e6ab0 177 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
4e4c362e
GS
178 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
179 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
d60c5a05
RD
180 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
181 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
4e4c362e
GS
182}
183
184# see if localization works on tied hashes
185tie %h, 'TH';
186%h = ('a' => 1, 'b' => 2, 'c' => 3);
187
188{
189 local($h{'a'}) = 'foo';
be6c24e0 190 local($h{'b'}) = $h{'b'};
159ad915
DM
191 local($h{'y'});
192 local($h{'z'}) = 33;
d441d3db
RD
193 is($h{'a'}, 'foo');
194 is($h{'b'}, 2);
4e4c362e
GS
195 local($h{'c'});
196 delete $h{'c'};
197}
d441d3db
RD
198is($h{'a'}, 1);
199is($h{'b'}, 2);
200is($h{'c'}, 3);
201# local() should preserve the existenceness of tied hash elements
202ok(! exists $h{'y'});
203ok(! exists $h{'z'});
d60c5a05
RD
204TODO: {
205 todo_skip("Localize entire tied hash");
206 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
207 local %h = %h;
208 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
209}
4e4c362e
GS
210
211@a = ('a', 'b', 'c');
212{
213 local($a[1]) = "X";
214 shift @a;
215}
d441d3db 216is($a[0].$a[1], "Xb");
4e4c362e 217
be6c24e0
GS
218# now try the same for %SIG
219
220$SIG{TERM} = 'foo';
221$SIG{INT} = \&foo;
222$SIG{__WARN__} = $SIG{INT};
223{
224 local($SIG{TERM}) = $SIG{TERM};
225 local($SIG{INT}) = $SIG{INT};
226 local($SIG{__WARN__}) = $SIG{__WARN__};
d441d3db
RD
227 is($SIG{TERM}, 'main::foo');
228 is($SIG{INT}, \&foo);
229 is($SIG{__WARN__}, \&foo);
be6c24e0
GS
230 local($SIG{INT});
231 delete $SIG{__WARN__};
232}
d441d3db
RD
233is($SIG{TERM}, 'main::foo');
234is($SIG{INT}, \&foo);
235is($SIG{__WARN__}, \&foo);
d60c5a05
RD
236{
237 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
238 local %SIG = %SIG;
239 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
240}
be6c24e0
GS
241
242# and for %ENV
243
244$ENV{_X_} = 'a';
245$ENV{_Y_} = 'b';
246$ENV{_Z_} = 'c';
247{
159ad915
DM
248 local($ENV{_A_});
249 local($ENV{_B_}) = 'foo';
be6c24e0
GS
250 local($ENV{_X_}) = 'foo';
251 local($ENV{_Y_}) = $ENV{_Y_};
d441d3db
RD
252 is($ENV{_X_}, 'foo');
253 is($ENV{_Y_}, 'b');
be6c24e0
GS
254 local($ENV{_Z_});
255 delete $ENV{_Z_};
256}
d441d3db
RD
257is($ENV{_X_}, 'a');
258is($ENV{_Y_}, 'b');
259is($ENV{_Z_}, 'c');
260# local() should preserve the existenceness of %ENV elements
261ok(! exists $ENV{_A_});
262ok(! exists $ENV{_B_});
13414bd5
JM
263
264SKIP: {
265 skip("Can't make list assignment to \%ENV on this system")
266 unless $list_assignment_supported;
d60c5a05
RD
267 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
268 local %ENV = %ENV;
269 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
270}
be6c24e0 271
0214ae40
GS
272# does implicit localization in foreach skip magic?
273
d441d3db 274$_ = "o 0,o 1,";
0214ae40
GS
275my $iter = 0;
276while (/(o.+?),/gc) {
d441d3db 277 is($1, "o $iter");
0214ae40 278 foreach (1..1) { $iter++ }
d441d3db 279 if ($iter > 2) { fail("endless loop"); last; }
0214ae40
GS
280}
281
282{
283 package UnderScore;
284 sub TIESCALAR { bless \my $self, shift }
285 sub FETCH { die "read \$_ forbidden" }
286 sub STORE { die "write \$_ forbidden" }
287 tie $_, __PACKAGE__;
0214ae40
GS
288 my @tests = (
289 "Nesting" => sub { print '#'; for (1..3) { print }
290 print "\n" }, 1,
291 "Reading" => sub { print }, 0,
292 "Matching" => sub { $x = /badness/ }, 0,
293 "Concat" => sub { $_ .= "a" }, 0,
294 "Chop" => sub { chop }, 0,
295 "Filetest" => sub { -x }, 0,
296 "Assignment" => sub { $_ = "Bad" }, 0,
297 # XXX whether next one should fail is debatable
298 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
299 "for local" => sub { for("#ok?\n"){ print } }, 1,
300 );
301 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
0214ae40 302 eval { &$code };
d441d3db 303 main::ok(($ok xor $@), "Underscore '$name'");
0214ae40
GS
304 }
305 untie $_;
306}
307
1f5346dc
SC
308{
309 # BUG 20001205.22
310 my %x;
311 $x{a} = 1;
312 { local $x{b} = 1; }
d441d3db 313 ok(! exists $x{b});
1f5346dc 314 { local @x{c,d,e}; }
d441d3db 315 ok(! exists $x{c});
1f5346dc 316}
159ad915 317
33f3c7b8
RGS
318# local() and readonly magic variables
319
320eval { local $1 = 1 };
d441d3db 321like($@, qr/Modification of a read-only value attempted/);
33f3c7b8
RGS
322
323eval { for ($1) { local $_ = 1 } };
d441d3db 324like($@, qr/Modification of a read-only value attempted/);
33f3c7b8 325
0cbee0a4 326# make sure $1 is still read-only
33f3c7b8 327eval { for ($1) { local $_ = 1 } };
d441d3db 328like($@, qr/Modification of a read-only value attempted/);
ac117f44
RGS
329
330# The s/// adds 'g' magic to $_, but it should remain non-readonly
331eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
d441d3db 332is($@, "");
4cb09e0a 333
503de470 334# RT #4342 Special local() behavior for $[
4cb09e0a
SP
335{
336 local $[ = 1;
503de470 337 ok(1 == $[, 'lexcical scope of local $[');
4cb09e0a
SP
338 f();
339}
340
341sub f { ok(0 == $[); }
342
985d6f61
HS
343# sub localisation
344{
345 package Other;
346
347 sub f1 { "f1" }
348 sub f2 { "f2" }
349
350 no warnings "redefine";
351 {
352 local *f1 = sub { "g1" };
353 ::ok(f1() eq "g1", "localised sub via glob");
354 }
355 ::ok(f1() eq "f1", "localised sub restored");
356 {
357 local $Other::{"f1"} = sub { "h1" };
358 ::ok(f1() eq "h1", "localised sub via stash");
359 }
360 ::ok(f1() eq "f1", "localised sub restored");
361 {
362 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
985d6f61
HS
363 ::ok(f1() eq "j1", "localised sub via stash slice");
364 ::ok(f2() eq "j2", "localised sub via stash slice");
985d6f61
HS
365 }
366 ::ok(f1() eq "f1", "localised sub restored");
367 ::ok(f2() eq "f2", "localised sub restored");
368}
7d654f43
NC
369
370# Localising unicode keys (bug #38815)
371{
372 my %h;
373 $h{"\243"} = "pound";
374 $h{"\302\240"} = "octects";
375 is(scalar keys %h, 2);
376 {
377 my $unicode = chr 256;
378 my $ambigous = "\240" . $unicode;
379 chop $ambigous;
380 local $h{$unicode} = 256;
381 local $h{$ambigous} = 160;
382
383 is(scalar keys %h, 4);
384 is($h{"\243"}, "pound");
385 is($h{$unicode}, 256);
386 is($h{$ambigous}, 160);
387 is($h{"\302\240"}, "octects");
388 }
389 is(scalar keys %h, 2);
390 is($h{"\243"}, "pound");
391 is($h{"\302\240"}, "octects");
392}
919acde0
NC
393
394# And with slices
395{
396 my %h;
397 $h{"\243"} = "pound";
398 $h{"\302\240"} = "octects";
399 is(scalar keys %h, 2);
400 {
401 my $unicode = chr 256;
402 my $ambigous = "\240" . $unicode;
403 chop $ambigous;
404 local @h{$unicode, $ambigous} = (256, 160);
405
406 is(scalar keys %h, 4);
407 is($h{"\243"}, "pound");
408 is($h{$unicode}, 256);
409 is($h{$ambigous}, 160);
410 is($h{"\302\240"}, "octects");
411 }
412 is(scalar keys %h, 2);
413 is($h{"\243"}, "pound");
414 is($h{"\302\240"}, "octects");
415}
658aef79
DM
416
417# [perl #39012] localizing @_ element then shifting frees element too # soon
418
419{
420 my $x;
421 my $y = bless [], 'X39012';
422 sub X39012::DESTROY { $x++ }
423 sub { local $_[0]; shift }->($y);
424 ok(!$x, '[perl #39012]');
425
426}
427
b2096149
BL
428# when localising a hash element, the key should be copied, not referenced
429
430{
431 my %h=('k1' => 111);
432 my $k='k1';
433 {
434 local $h{$k}=222;
435
436 is($h{'k1'},222);
437 $k='k2';
438 }
439 ok(! exists($h{'k2'}));
440 is($h{'k1'},111);
441}
46c458a0
HS
442{
443 my %h=('k1' => 111);
444 our $k = 'k1'; # try dynamic too
445 {
446 local $h{$k}=222;
447 is($h{'k1'},222);
448 $k='k2';
449 }
450 ok(! exists($h{'k2'}));
451 is($h{'k1'},111);
452}
72651472 453
07a28ea7
B
454like( runperl(stderr => 1,
455 prog => 'use constant foo => q(a);' .
456 'index(q(a), foo);' .
45f2a18c 457 'local *g=${::}{foo};print q(ok);'), "ok", "[perl #52740]");
07a28ea7 458
72651472
NC
459# Keep this test last, as it can SEGV
460{
461 local *@;
462 pass("Localised *@");
463 eval {1};
464 pass("Can eval with *@ localised");
465}
466