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 @a = ('a', 'b', 'c');
172 ok(!exists($a[888]));
173 delete local $a[888];
175 ok(!exists($a[888]));
177 ok(!exists($a[999]));
178 my ($d, $zzz) = delete local @a[4, 999];
181 ok(!exists($a[999]));
185 my $c = delete local $a[2];
200 ok(!exists($a[888]));
201 ok(!exists($a[999]));
203 %h = (a => 1, b => 2, c => 3, d => 4);
206 is(scalar(keys(%h)), 3);
212 ok(!exists($h{yyy}));
213 delete local $h{yyy};
214 is(scalar(keys(%h)), 3);
215 ok(!exists($h{yyy}));
217 ok(!exists($h{zzz}));
218 my ($d, $zzz) = delete local @h{qw/d zzz/};
219 is(scalar(keys(%h)), 2);
221 ok(!exists($h{zzz}));
225 my $c = delete local $h{c};
226 is(scalar(keys(%h)), 1);
233 is(scalar(keys(%h)), 4);
238 ok(!exists($h{yyy}));
239 ok(!exists($h{zzz}));
241 %h = ('a' => { 'b' => 1 }, 'c' => 2);
243 my $a = delete local $h{a};
244 is(scalar(keys(%h)), 1);
247 is(scalar(keys(%$a)), 1);
249 my $b = delete local $a->{b};
250 is(scalar(keys(%$a)), 0);
255 is(scalar(keys(%h)), 2);
258 is(scalar(keys(%$a)), 2);
264 %h = ('a' => 1, 'b' => 2, 'c' => 3);
266 local($h{'a'}) = 'foo';
267 local($h{'b'}) = $h{'b'};
276 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
278 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
282 # check for scope leakage
284 if (1) { local $a = 'inner' }
287 # see if localization works when scope unwinds
297 # see if localization works on tied arrays
300 sub TIEARRAY { bless [], $_[0] }
301 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
302 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
303 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->[$_[1]]; }
304 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->[$_[1]]; }
305 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
306 sub FETCHSIZE { scalar(@{$_[0]}) }
307 sub SHIFT { shift (@{$_[0]}) }
312 @a = ('a', 'b', 'c');
314 local($a[1]) = 'foo';
315 local($a[2]) = $a[2];
329 # local() should preserve the existenceness of tied array elements
330 @a = ('a', 'b', 'c');
340 @a = ('a', 'b', 'c');
353 @a = ('a', 'b', 'c');
355 local(@a[4,6]) = ('x', 'z');
367 @a = ('a', 'b', 'c');
369 local(@a[4,6]) = ('x', 'z');
382 @a = ('a', 'b', 'c');
393 ok(!exists($a[888]));
394 delete local $a[888];
396 ok(!exists($a[888]));
398 ok(!exists($a[999]));
399 my ($d, $zzz) = delete local @a[4, 999];
402 ok(!exists($a[999]));
406 my $c = delete local $a[2];
421 ok(!exists($a[888]));
422 ok(!exists($a[999]));
424 # see if localization works on tied hashes
427 sub TIEHASH { bless {}, $_[0] }
428 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
429 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
430 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
431 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
432 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
433 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
434 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
438 %h = ('a' => 1, 'b' => 2, 'c' => 3);
441 local($h{'a'}) = 'foo';
442 local($h{'b'}) = $h{'b'};
453 # local() should preserve the existenceness of tied hash elements
454 ok(! exists $h{'y'});
455 ok(! exists $h{'z'});
457 todo_skip("Localize entire tied hash");
458 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
460 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
463 %h = (a => 1, b => 2, c => 3, d => 4);
466 is(scalar(keys(%h)), 3);
472 ok(!exists($h{yyy}));
473 delete local $h{yyy};
474 is(scalar(keys(%h)), 3);
475 ok(!exists($h{yyy}));
477 ok(!exists($h{zzz}));
478 my ($d, $zzz) = delete local @h{qw/d zzz/};
479 is(scalar(keys(%h)), 2);
481 ok(!exists($h{zzz}));
485 my $c = delete local $h{c};
486 is(scalar(keys(%h)), 1);
493 is(scalar(keys(%h)), 4);
498 ok(!exists($h{yyy}));
499 ok(!exists($h{zzz}));
501 @a = ('a', 'b', 'c');
506 is($a[0].$a[1], "Xb");
508 # now try the same for %SIG
512 $SIG{__WARN__} = $SIG{INT};
514 local($SIG{TERM}) = $SIG{TERM};
515 local($SIG{INT}) = $SIG{INT};
516 local($SIG{__WARN__}) = $SIG{__WARN__};
517 is($SIG{TERM}, 'main::foo');
518 is($SIG{INT}, \&foo);
519 is($SIG{__WARN__}, \&foo);
521 delete $SIG{__WARN__};
523 is($SIG{TERM}, 'main::foo');
524 is($SIG{INT}, \&foo);
525 is($SIG{__WARN__}, \&foo);
527 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
529 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
539 local($ENV{_B_}) = 'foo';
540 local($ENV{_X_}) = 'foo';
541 local($ENV{_Y_}) = $ENV{_Y_};
542 is($ENV{_X_}, 'foo');
550 # local() should preserve the existenceness of %ENV elements
551 ok(! exists $ENV{_A_});
552 ok(! exists $ENV{_B_});
555 skip("Can't make list assignment to \%ENV on this system")
556 unless $list_assignment_supported;
557 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
559 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
562 # does implicit localization in foreach skip magic?
566 while (/(o.+?),/gc) {
568 foreach (1..1) { $iter++ }
569 if ($iter > 2) { fail("endless loop"); last; }
574 sub TIESCALAR { bless \my $self, shift }
575 sub FETCH { die "read \$_ forbidden" }
576 sub STORE { die "write \$_ forbidden" }
579 "Nesting" => sub { print '#'; for (1..3) { print }
581 "Reading" => sub { print }, 0,
582 "Matching" => sub { $x = /badness/ }, 0,
583 "Concat" => sub { $_ .= "a" }, 0,
584 "Chop" => sub { chop }, 0,
585 "Filetest" => sub { -x }, 0,
586 "Assignment" => sub { $_ = "Bad" }, 0,
587 # XXX whether next one should fail is debatable
588 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
589 "for local" => sub { for("#ok?\n"){ print } }, 1,
591 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
593 main::ok(($ok xor $@), "Underscore '$name'");
608 # local() and readonly magic variables
610 eval { local $1 = 1 };
611 like($@, qr/Modification of a read-only value attempted/);
613 eval { for ($1) { local $_ = 1 } };
614 like($@, qr/Modification of a read-only value attempted/);
616 # make sure $1 is still read-only
617 eval { for ($1) { local $_ = 1 } };
618 like($@, qr/Modification of a read-only value attempted/);
620 # The s/// adds 'g' magic to $_, but it should remain non-readonly
621 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
624 # RT #4342 Special local() behavior for $[
627 ok(1 == $[, 'lexcical scope of local $[');
631 sub f { ok(0 == $[); }
640 no warnings "redefine";
642 local *f1 = sub { "g1" };
643 ::ok(f1() eq "g1", "localised sub via glob");
645 ::ok(f1() eq "f1", "localised sub restored");
647 local $Other::{"f1"} = sub { "h1" };
648 ::ok(f1() eq "h1", "localised sub via stash");
650 ::ok(f1() eq "f1", "localised sub restored");
652 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
653 ::ok(f1() eq "j1", "localised sub via stash slice");
654 ::ok(f2() eq "j2", "localised sub via stash slice");
656 ::ok(f1() eq "f1", "localised sub restored");
657 ::ok(f2() eq "f2", "localised sub restored");
660 # Localising unicode keys (bug #38815)
663 $h{"\243"} = "pound";
664 $h{"\302\240"} = "octects";
665 is(scalar keys %h, 2);
667 my $unicode = chr 256;
668 my $ambigous = "\240" . $unicode;
670 local $h{$unicode} = 256;
671 local $h{$ambigous} = 160;
673 is(scalar keys %h, 4);
674 is($h{"\243"}, "pound");
675 is($h{$unicode}, 256);
676 is($h{$ambigous}, 160);
677 is($h{"\302\240"}, "octects");
679 is(scalar keys %h, 2);
680 is($h{"\243"}, "pound");
681 is($h{"\302\240"}, "octects");
687 $h{"\243"} = "pound";
688 $h{"\302\240"} = "octects";
689 is(scalar keys %h, 2);
691 my $unicode = chr 256;
692 my $ambigous = "\240" . $unicode;
694 local @h{$unicode, $ambigous} = (256, 160);
696 is(scalar keys %h, 4);
697 is($h{"\243"}, "pound");
698 is($h{$unicode}, 256);
699 is($h{$ambigous}, 160);
700 is($h{"\302\240"}, "octects");
702 is(scalar keys %h, 2);
703 is($h{"\243"}, "pound");
704 is($h{"\302\240"}, "octects");
707 # [perl #39012] localizing @_ element then shifting frees element too # soon
711 my $y = bless [], 'X39012';
712 sub X39012::DESTROY { $x++ }
713 sub { local $_[0]; shift }->($y);
714 ok(!$x, '[perl #39012]');
718 # when localising a hash element, the key should be copied, not referenced
729 ok(! exists($h{'k2'}));
734 our $k = 'k1'; # try dynamic too
740 ok(! exists($h{'k2'}));
744 like( runperl(stderr => 1,
745 prog => 'use constant foo => q(a);' .
746 'index(q(a), foo);' .
747 'local *g=${::}{foo};print q(ok);'), "ok", "[perl #52740]");
749 # Keep this test last, as it can SEGV
752 pass("Localised *@");
754 pass("Can eval with *@ localised");