6 set_up_inc( qw(. ../lib) );
10 my $list_assignment_supported = 1;
12 #mg.c says list assignment not supported on VMS 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];
328 # RT #7938: localising an array should make it temporarily untied
332 is("@a", "6 7 8", 'local @a assigned 6,7,8');
335 local *TA::STORE = sub { $c++ };
337 is($c, 0, 'STORE not called after array localised');
339 is("@a", "9 7 8", 'local @a should now be 9 7 8');
341 is("@a", "a b c", '@a should now contain original value');
344 # local() should preserve the existenceness of tied array elements
345 @a = ('a', 'b', 'c');
355 @a = ('a', 'b', 'c');
368 @a = ('a', 'b', 'c');
370 local(@a[4,6]) = ('x', 'z');
382 @a = ('a', 'b', 'c');
384 local(@a[4,6]) = ('x', 'z');
397 @a = ('a', 'b', 'c');
408 ok(!exists($a[888]));
409 delete local $a[888];
411 ok(!exists($a[888]));
413 ok(!exists($a[999]));
414 my ($d, $zzz) = delete local @a[4, 999];
417 ok(!exists($a[999]));
421 my $c = delete local $a[2];
436 ok(!exists($a[888]));
437 ok(!exists($a[999]));
439 # see if localization works on tied hashes
442 sub TIEHASH { bless {}, $_[0] }
443 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
444 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
445 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
446 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
447 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
448 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
449 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
453 %h = ('a' => 1, 'b' => 2, 'c' => 3);
456 local($h{'a'}) = 'foo';
457 local($h{'b'}) = $h{'b'};
469 # local() should preserve the existenceness of tied hash elements
470 ok(! exists $h{'y'});
471 ok(! exists $h{'z'});
473 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
475 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
478 # RT #7939: localising a hash should make it temporarily untied
480 %h = qw(a 1 b 2 c 3);
481 local %h = qw(x 6 y 7 z 8);
482 is(join('', sort keys %h), "xyz", 'local %h has new keys');
483 is(join('', sort values %h), "678", 'local %h has new values');
486 local *TH::STORE = sub { $c++ };
488 is($c, 0, 'STORE not called after hash localised');
490 is($h{x}, 9, '$h{x} should now be 9');
492 is(join('', sort keys %h), "abc", 'restored %h has original keys');
493 is(join('', sort values %h), "123", 'restored %h has original values');
496 %h = (a => 1, b => 2, c => 3, d => 4);
499 is(scalar(keys(%h)), 3);
505 ok(!exists($h{yyy}));
506 delete local $h{yyy};
507 is(scalar(keys(%h)), 3);
508 ok(!exists($h{yyy}));
510 ok(!exists($h{zzz}));
511 my ($d, $zzz) = delete local @h{qw/d zzz/};
512 is(scalar(keys(%h)), 2);
514 ok(!exists($h{zzz}));
518 my $c = delete local $h{c};
519 is(scalar(keys(%h)), 1);
526 is(scalar(keys(%h)), 4);
531 ok(!exists($h{yyy}));
532 ok(!exists($h{zzz}));
534 @a = ('a', 'b', 'c');
539 is($a[0].$a[1], "Xb");
541 # now try the same for %SIG
545 $SIG{__WARN__} = $SIG{INT};
547 local($SIG{TERM}) = $SIG{TERM};
548 local($SIG{INT}) = $SIG{INT};
549 local($SIG{__WARN__}) = $SIG{__WARN__};
550 is($SIG{TERM}, 'main::foo');
551 is($SIG{INT}, \&foo);
552 is($SIG{__WARN__}, \&foo);
554 delete $SIG{__WARN__};
556 is($SIG{TERM}, 'main::foo');
557 is($SIG{INT}, \&foo);
558 is($SIG{__WARN__}, \&foo);
560 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
562 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
572 local($ENV{_B_}) = 'foo';
573 local($ENV{_X_}) = 'foo';
574 local($ENV{_Y_}) = $ENV{_Y_};
575 is($ENV{_X_}, 'foo');
583 # local() should preserve the existenceness of %ENV elements
584 ok(! exists $ENV{_A_});
585 ok(! exists $ENV{_B_});
588 skip("Can't make list assignment to \%ENV on this system")
589 unless $list_assignment_supported;
590 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
592 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
595 # does implicit localization in foreach skip magic?
599 while (/(o.+?),/gc) {
601 foreach (1..1) { $iter++ }
602 if ($iter > 2) { fail("endless loop"); last; }
607 sub TIESCALAR { bless \my $self, shift }
608 sub FETCH { die "read \$_ forbidden" }
609 sub STORE { die "write \$_ forbidden" }
612 "Nesting" => sub { my $x = '#'; for (1..3) { $x .= $_ }
614 "Reading" => sub { print }, 0,
615 "Matching" => sub { $x = /badness/ }, 0,
616 "Concat" => sub { $_ .= "a" }, 0,
617 "Chop" => sub { chop }, 0,
618 "Filetest" => sub { -x }, 0,
619 "Assignment" => sub { $_ = "Bad" }, 0,
620 "for local" => sub { for("#ok?\n"){ print } }, 1,
622 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
624 main::ok(($ok xor $@), "Underscore '$name'");
630 # BUG 20001205.022 (RT #4852)
639 # local() and readonly magic variables
641 eval { local $1 = 1 };
642 like($@, qr/Modification of a read-only value attempted/);
644 # local($_) always strips all magic
645 eval { for ($1) { local $_ = 1 } };
649 my $STORE = my $FETCH = 0;
651 sub TIEHASH { bless $_[1], $_[0] }
652 sub FETCH { ++$FETCH; 42 }
653 sub STORE { ++$STORE }
656 tie my %hash, "TieHash", {};
658 eval { for ($hash{key}) {local $_ = 2} };
663 # The s/// adds 'g' magic to $_, but it should remain non-readonly
664 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
676 no warnings "redefine";
678 local *f1 = sub { "g1" };
679 ::ok(f1() eq "g1", "localised sub via glob");
681 ::ok(f1() eq "f1", "localised sub restored");
683 local $Other::{"f1"} = sub { "h1" };
684 ::ok(f1() eq "h1", "localised sub via stash");
686 ::ok(f1() eq "f1", "localised sub restored");
687 # Do that test again, but with a different glob, to make sure that
688 # localisation via multideref can handle a subref in a stash.
689 # (The local *f1 above will have ensured that we have a full glob,
692 local $Other::{"f3"} = sub { "h1" };
693 ::ok(f3() eq "h1", "localised sub via stash");
695 ::ok(f3() eq "f3", "localised sub restored");
696 # Also, we need to test pp_helem, which we can do by using a more
699 local $Other::{${\"f4"}} = sub { "h1" };
700 ::ok(f4() eq "h1", "localised sub via stash");
702 ::ok(f4() eq "f4", "localised sub restored");
704 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
705 ::ok(f1() eq "j1", "localised sub via stash slice");
706 ::ok(f2() eq "j2", "localised sub via stash slice");
708 ::ok(f1() eq "f1", "localised sub restored");
709 ::ok(f2() eq "f2", "localised sub restored");
712 # Localising unicode keys (bug #38815)
715 $h{"\243"} = "pound";
716 $h{"\302\240"} = "octects";
717 is(scalar keys %h, 2);
719 my $unicode = chr 256;
720 my $ambigous = "\240" . $unicode;
722 local $h{$unicode} = 256;
723 local $h{$ambigous} = 160;
725 is(scalar keys %h, 4);
726 is($h{"\243"}, "pound");
727 is($h{$unicode}, 256);
728 is($h{$ambigous}, 160);
729 is($h{"\302\240"}, "octects");
731 is(scalar keys %h, 2);
732 is($h{"\243"}, "pound");
733 is($h{"\302\240"}, "octects");
739 $h{"\243"} = "pound";
740 $h{"\302\240"} = "octects";
741 is(scalar keys %h, 2);
743 my $unicode = chr 256;
744 my $ambigous = "\240" . $unicode;
746 local @h{$unicode, $ambigous} = (256, 160);
748 is(scalar keys %h, 4);
749 is($h{"\243"}, "pound");
750 is($h{$unicode}, 256);
751 is($h{$ambigous}, 160);
752 is($h{"\302\240"}, "octects");
754 is(scalar keys %h, 2);
755 is($h{"\243"}, "pound");
756 is($h{"\302\240"}, "octects");
759 # [perl #39012] localizing @_ element then shifting frees element too # soon
763 my $y = bless [], 'X39012';
764 sub X39012::DESTROY { $x++ }
765 sub { local $_[0]; shift }->($y);
766 ok(!$x, '[perl #39012]');
770 # when localising a hash element, the key should be copied, not referenced
781 ok(! exists($h{'k2'}));
786 our $k = 'k1'; # try dynamic too
792 ok(! exists($h{'k2'}));
796 like( runperl(stderr => 1,
797 prog => 'use constant foo => q(a);' .
798 'index(q(a), foo);' .
799 'local *g=${::}{foo};print q(ok);'), qr/^ok$/, "[perl #52740]");
801 # related to perl #112966
802 # Magic should not cause elements not to be deleted after scope unwinding
803 # when they did not exist before local()
804 () = \$#squinch; # $#foo in lvalue context makes array magical
807 local @squinch[1..2];
809 m??; # makes stash magical
810 local $Flibbert::{foo};
811 local @Flibbert::{<bar baz>};
813 ok !exists $Flibbert::{foo},
814 'local helem on magic hash does not leave elems on scope exit';
815 ok !exists $Flibbert::{bar},
816 'local hslice on magic hash does not leave elems on scope exit';
817 ok !exists $squinch[0],
818 'local aelem on magic hash does not leave elems on scope exit';
819 ok !exists $squinch[1],
820 'local aslice on magic hash does not leave elems on scope exit';
822 # Keep these tests last, as they can SEGV
825 pass("Localised *@");
827 pass("Can eval with *@ localised");
831 delete $::{$_} for 'nugguton','netgonch';
833 pass ('localised arrays and hashes do not crash if glob is deleted');
835 # [perl #112966] Rmagic can cause delete local to crash
837 local $SIG{__WARN__};
838 delete local $ISA[0];
839 delete local @ISA[1..10];
840 m??; # makes stash magical
841 delete local $Grompits::{foo};
842 delete local @Grompits::{<foo bar>};
844 pass 'rmagic does not cause delete local to crash on nonexistent elems';
850 is($#a, 2, 'RT #7411: local($#a) should change count');
851 is("@a", '1 2 3', 'RT #7411: local($#a) should shorten array');
854 local $::TODO = 'RT #7411: local($#a)';
856 is($#a, 4, 'RT #7411: after local($#a), count should be restored');
857 is("@a", '1 2 3 4 5', 'RT #7411: after local($#a), array should be restored');
862 local $::TODO = 'RT #7615: if (local $a)';
865 is($a, 10, 'RT #7615: local in if condition should be restored');