10 my $list_assignment_supported = 1;
12 #mg.c says list assignment not supported on VMS, EPOC, and SYMBIAN.
13 $list_assignment_supported = 0 if ($^O eq 'VMS');
21 { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
33 @res = &foo("a 1","b 2");
44 # same thing, only with arrays and associative arrays
51 { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
62 @res = &foo2("a 1","b 2");
75 like($@, qr/Can't localize through a reference/);
77 eval '$e = []; local(@$e)';
78 like($@, qr/Can't localize through a reference/);
80 eval '$e = {}; local(%$e)';
81 like($@, qr/Can't localize through a reference/);
83 # Array and hash elements
102 is($a[0].$a[1], "Xb");
109 %h = ('a' => 1, 'b' => 2, 'c' => 3);
111 local($h{'a'}) = 'foo';
112 local($h{'b'}) = $h{'b'};
121 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
123 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
127 # check for scope leakage
129 if (1) { local $a = 'inner' }
132 # see if localization works when scope unwinds
142 # see if localization works on tied arrays
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]}) }
155 @a = ('a', 'b', 'c');
157 local($a[1]) = 'foo';
158 local($a[2]) = $a[2];
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 }
177 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
178 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
179 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
180 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
181 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
184 # see if localization works on tied hashes
186 %h = ('a' => 1, 'b' => 2, 'c' => 3);
189 local($h{'a'}) = 'foo';
190 local($h{'b'}) = $h{'b'};
201 # local() should preserve the existenceness of tied hash elements
202 ok(! exists $h{'y'});
203 ok(! exists $h{'z'});
205 todo_skip("Localize entire tied hash");
206 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
208 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
211 @a = ('a', 'b', 'c');
216 is($a[0].$a[1], "Xb");
218 # now try the same for %SIG
222 $SIG{__WARN__} = $SIG{INT};
224 local($SIG{TERM}) = $SIG{TERM};
225 local($SIG{INT}) = $SIG{INT};
226 local($SIG{__WARN__}) = $SIG{__WARN__};
227 is($SIG{TERM}, 'main::foo');
228 is($SIG{INT}, \&foo);
229 is($SIG{__WARN__}, \&foo);
231 delete $SIG{__WARN__};
233 is($SIG{TERM}, 'main::foo');
234 is($SIG{INT}, \&foo);
235 is($SIG{__WARN__}, \&foo);
237 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
239 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
249 local($ENV{_B_}) = 'foo';
250 local($ENV{_X_}) = 'foo';
251 local($ENV{_Y_}) = $ENV{_Y_};
252 is($ENV{_X_}, 'foo');
260 # local() should preserve the existenceness of %ENV elements
261 ok(! exists $ENV{_A_});
262 ok(! exists $ENV{_B_});
265 skip("Can't make list assignment to \%ENV on this system")
266 unless $list_assignment_supported;
267 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
269 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
272 # does implicit localization in foreach skip magic?
276 while (/(o.+?),/gc) {
278 foreach (1..1) { $iter++ }
279 if ($iter > 2) { fail("endless loop"); last; }
284 sub TIESCALAR { bless \my $self, shift }
285 sub FETCH { die "read \$_ forbidden" }
286 sub STORE { die "write \$_ forbidden" }
289 "Nesting" => sub { print '#'; for (1..3) { print }
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,
301 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
303 main::ok(($ok xor $@), "Underscore '$name'");
318 # local() and readonly magic variables
320 eval { local $1 = 1 };
321 like($@, qr/Modification of a read-only value attempted/);
323 eval { for ($1) { local $_ = 1 } };
324 like($@, qr/Modification of a read-only value attempted/);
326 # make sure $1 is still read-only
327 eval { for ($1) { local $_ = 1 } };
328 like($@, qr/Modification of a read-only value attempted/);
330 # The s/// adds 'g' magic to $_, but it should remain non-readonly
331 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
334 # Special local() behavior for $[
335 # (see RT #38207 - Useless localization of constant ($[) in getopts.pl}
338 local $TODO = "local() not currently working correctly with \$[";
344 sub f { ok(0 == $[); }
353 no warnings "redefine";
355 local *f1 = sub { "g1" };
356 ::ok(f1() eq "g1", "localised sub via glob");
358 ::ok(f1() eq "f1", "localised sub restored");
360 local $Other::{"f1"} = sub { "h1" };
361 ::ok(f1() eq "h1", "localised sub via stash");
363 ::ok(f1() eq "f1", "localised sub restored");
365 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
366 ::ok(f1() eq "j1", "localised sub via stash slice");
367 ::ok(f2() eq "j2", "localised sub via stash slice");
369 ::ok(f1() eq "f1", "localised sub restored");
370 ::ok(f2() eq "f2", "localised sub restored");
373 # Localising unicode keys (bug #38815)
376 $h{"\243"} = "pound";
377 $h{"\302\240"} = "octects";
378 is(scalar keys %h, 2);
380 my $unicode = chr 256;
381 my $ambigous = "\240" . $unicode;
383 local $h{$unicode} = 256;
384 local $h{$ambigous} = 160;
386 is(scalar keys %h, 4);
387 is($h{"\243"}, "pound");
388 is($h{$unicode}, 256);
389 is($h{$ambigous}, 160);
390 is($h{"\302\240"}, "octects");
392 is(scalar keys %h, 2);
393 is($h{"\243"}, "pound");
394 is($h{"\302\240"}, "octects");
400 $h{"\243"} = "pound";
401 $h{"\302\240"} = "octects";
402 is(scalar keys %h, 2);
404 my $unicode = chr 256;
405 my $ambigous = "\240" . $unicode;
407 local @h{$unicode, $ambigous} = (256, 160);
409 is(scalar keys %h, 4);
410 is($h{"\243"}, "pound");
411 is($h{$unicode}, 256);
412 is($h{$ambigous}, 160);
413 is($h{"\302\240"}, "octects");
415 is(scalar keys %h, 2);
416 is($h{"\243"}, "pound");
417 is($h{"\302\240"}, "octects");
420 # [perl #39012] localizing @_ element then shifting frees element too # soon
424 my $y = bless [], 'X39012';
425 sub X39012::DESTROY { $x++ }
426 sub { local $_[0]; shift }->($y);
427 ok(!$x, '[perl #39012]');
431 # when localising a hash element, the key should be copied, not referenced
442 ok(! exists($h{'k2'}));