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
107 @a = ('a', 'b', 'c');
120 @a = ('a', 'b', 'c');
122 local(@a[4,6]) = ('x', 'z');
134 @a = ('a', 'b', 'c');
136 local(@a[4,6]) = ('x', 'z');
149 @a = ('a', 'b', 'c');
154 is($a[0].$a[1], "Xb");
161 %h = ('a' => 1, 'b' => 2, 'c' => 3);
163 local($h{'a'}) = 'foo';
164 local($h{'b'}) = $h{'b'};
173 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
175 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
179 # check for scope leakage
181 if (1) { local $a = 'inner' }
184 # see if localization works when scope unwinds
194 # see if localization works on tied arrays
197 sub TIEARRAY { bless [], $_[0] }
198 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
199 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
200 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->[$_[1]]; }
201 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->[$_[1]]; }
202 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
203 sub FETCHSIZE { scalar(@{$_[0]}) }
204 sub SHIFT { shift (@{$_[0]}) }
209 @a = ('a', 'b', 'c');
211 local($a[1]) = 'foo';
212 local($a[2]) = $a[2];
226 # local() should preserve the existenceness of tied array elements
227 @a = ('a', 'b', 'c');
237 @a = ('a', 'b', 'c');
250 @a = ('a', 'b', 'c');
252 local(@a[4,6]) = ('x', 'z');
264 @a = ('a', 'b', 'c');
266 local(@a[4,6]) = ('x', 'z');
279 # see if localization works on tied hashes
282 sub TIEHASH { bless {}, $_[0] }
283 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
284 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
285 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
286 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
287 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
288 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
289 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
293 %h = ('a' => 1, 'b' => 2, 'c' => 3);
296 local($h{'a'}) = 'foo';
297 local($h{'b'}) = $h{'b'};
308 # local() should preserve the existenceness of tied hash elements
309 ok(! exists $h{'y'});
310 ok(! exists $h{'z'});
312 todo_skip("Localize entire tied hash");
313 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
315 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
318 @a = ('a', 'b', 'c');
323 is($a[0].$a[1], "Xb");
325 # now try the same for %SIG
329 $SIG{__WARN__} = $SIG{INT};
331 local($SIG{TERM}) = $SIG{TERM};
332 local($SIG{INT}) = $SIG{INT};
333 local($SIG{__WARN__}) = $SIG{__WARN__};
334 is($SIG{TERM}, 'main::foo');
335 is($SIG{INT}, \&foo);
336 is($SIG{__WARN__}, \&foo);
338 delete $SIG{__WARN__};
340 is($SIG{TERM}, 'main::foo');
341 is($SIG{INT}, \&foo);
342 is($SIG{__WARN__}, \&foo);
344 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
346 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
356 local($ENV{_B_}) = 'foo';
357 local($ENV{_X_}) = 'foo';
358 local($ENV{_Y_}) = $ENV{_Y_};
359 is($ENV{_X_}, 'foo');
367 # local() should preserve the existenceness of %ENV elements
368 ok(! exists $ENV{_A_});
369 ok(! exists $ENV{_B_});
372 skip("Can't make list assignment to \%ENV on this system")
373 unless $list_assignment_supported;
374 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
376 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
379 # does implicit localization in foreach skip magic?
383 while (/(o.+?),/gc) {
385 foreach (1..1) { $iter++ }
386 if ($iter > 2) { fail("endless loop"); last; }
391 sub TIESCALAR { bless \my $self, shift }
392 sub FETCH { die "read \$_ forbidden" }
393 sub STORE { die "write \$_ forbidden" }
396 "Nesting" => sub { print '#'; for (1..3) { print }
398 "Reading" => sub { print }, 0,
399 "Matching" => sub { $x = /badness/ }, 0,
400 "Concat" => sub { $_ .= "a" }, 0,
401 "Chop" => sub { chop }, 0,
402 "Filetest" => sub { -x }, 0,
403 "Assignment" => sub { $_ = "Bad" }, 0,
404 # XXX whether next one should fail is debatable
405 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
406 "for local" => sub { for("#ok?\n"){ print } }, 1,
408 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
410 main::ok(($ok xor $@), "Underscore '$name'");
425 # local() and readonly magic variables
427 eval { local $1 = 1 };
428 like($@, qr/Modification of a read-only value attempted/);
430 eval { for ($1) { local $_ = 1 } };
431 like($@, qr/Modification of a read-only value attempted/);
433 # make sure $1 is still read-only
434 eval { for ($1) { local $_ = 1 } };
435 like($@, qr/Modification of a read-only value attempted/);
437 # The s/// adds 'g' magic to $_, but it should remain non-readonly
438 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
441 # RT #4342 Special local() behavior for $[
444 ok(1 == $[, 'lexcical scope of local $[');
448 sub f { ok(0 == $[); }
457 no warnings "redefine";
459 local *f1 = sub { "g1" };
460 ::ok(f1() eq "g1", "localised sub via glob");
462 ::ok(f1() eq "f1", "localised sub restored");
464 local $Other::{"f1"} = sub { "h1" };
465 ::ok(f1() eq "h1", "localised sub via stash");
467 ::ok(f1() eq "f1", "localised sub restored");
469 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
470 ::ok(f1() eq "j1", "localised sub via stash slice");
471 ::ok(f2() eq "j2", "localised sub via stash slice");
473 ::ok(f1() eq "f1", "localised sub restored");
474 ::ok(f2() eq "f2", "localised sub restored");
477 # Localising unicode keys (bug #38815)
480 $h{"\243"} = "pound";
481 $h{"\302\240"} = "octects";
482 is(scalar keys %h, 2);
484 my $unicode = chr 256;
485 my $ambigous = "\240" . $unicode;
487 local $h{$unicode} = 256;
488 local $h{$ambigous} = 160;
490 is(scalar keys %h, 4);
491 is($h{"\243"}, "pound");
492 is($h{$unicode}, 256);
493 is($h{$ambigous}, 160);
494 is($h{"\302\240"}, "octects");
496 is(scalar keys %h, 2);
497 is($h{"\243"}, "pound");
498 is($h{"\302\240"}, "octects");
504 $h{"\243"} = "pound";
505 $h{"\302\240"} = "octects";
506 is(scalar keys %h, 2);
508 my $unicode = chr 256;
509 my $ambigous = "\240" . $unicode;
511 local @h{$unicode, $ambigous} = (256, 160);
513 is(scalar keys %h, 4);
514 is($h{"\243"}, "pound");
515 is($h{$unicode}, 256);
516 is($h{$ambigous}, 160);
517 is($h{"\302\240"}, "octects");
519 is(scalar keys %h, 2);
520 is($h{"\243"}, "pound");
521 is($h{"\302\240"}, "octects");
524 # [perl #39012] localizing @_ element then shifting frees element too # soon
528 my $y = bless [], 'X39012';
529 sub X39012::DESTROY { $x++ }
530 sub { local $_[0]; shift }->($y);
531 ok(!$x, '[perl #39012]');
535 # when localising a hash element, the key should be copied, not referenced
546 ok(! exists($h{'k2'}));
551 our $k = 'k1'; # try dynamic too
557 ok(! exists($h{'k2'}));
561 like( runperl(stderr => 1,
562 prog => 'use constant foo => q(a);' .
563 'index(q(a), foo);' .
564 'local *g=${::}{foo};print q(ok);'), "ok", "[perl #52740]");
566 # Keep this test last, as it can SEGV
569 pass("Localised *@");
571 pass("Can eval with *@ localised");