9 my $list_assignment_supported = 1;
11 #mg.c says list assignment not supported on VMS, EPOC, and SYMBIAN.
12 $list_assignment_supported = 0 if ($^O eq 'VMS');
20 { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
32 @res = &foo("a 1","b 2");
43 # same thing, only with arrays and associative arrays
50 { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
61 @res = &foo2("a 1","b 2");
74 like($@, qr/Can't localize through a reference/);
76 eval '$e = []; local(@$e)';
77 like($@, qr/Can't localize through a reference/);
79 eval '$e = {}; local(%$e)';
80 like($@, qr/Can't localize through a reference/);
82 # Array and hash elements
101 is($a[0].$a[1], "Xb");
108 %h = ('a' => 1, 'b' => 2, 'c' => 3);
110 local($h{'a'}) = 'foo';
111 local($h{'b'}) = $h{'b'};
120 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
122 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
126 # check for scope leakage
128 if (1) { local $a = 'inner' }
131 # see if localization works when scope unwinds
141 # see if localization works on tied arrays
144 sub TIEARRAY { bless [], $_[0] }
145 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
146 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
147 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
148 sub FETCHSIZE { scalar(@{$_[0]}) }
149 sub SHIFT { shift (@{$_[0]}) }
154 @a = ('a', 'b', 'c');
156 local($a[1]) = 'foo';
157 local($a[2]) = $a[2];
173 sub TIEHASH { bless {}, $_[0] }
174 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
175 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
176 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
177 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
178 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
179 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
180 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
183 # see if localization works on tied hashes
185 %h = ('a' => 1, 'b' => 2, 'c' => 3);
188 local($h{'a'}) = 'foo';
189 local($h{'b'}) = $h{'b'};
200 # local() should preserve the existenceness of tied hash elements
201 ok(! exists $h{'y'});
202 ok(! exists $h{'z'});
204 todo_skip("Localize entire tied hash");
205 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
207 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
210 @a = ('a', 'b', 'c');
215 is($a[0].$a[1], "Xb");
217 # now try the same for %SIG
221 $SIG{__WARN__} = $SIG{INT};
223 local($SIG{TERM}) = $SIG{TERM};
224 local($SIG{INT}) = $SIG{INT};
225 local($SIG{__WARN__}) = $SIG{__WARN__};
226 is($SIG{TERM}, 'main::foo');
227 is($SIG{INT}, \&foo);
228 is($SIG{__WARN__}, \&foo);
230 delete $SIG{__WARN__};
232 is($SIG{TERM}, 'main::foo');
233 is($SIG{INT}, \&foo);
234 is($SIG{__WARN__}, \&foo);
236 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
238 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
248 local($ENV{_B_}) = 'foo';
249 local($ENV{_X_}) = 'foo';
250 local($ENV{_Y_}) = $ENV{_Y_};
251 is($ENV{_X_}, 'foo');
259 # local() should preserve the existenceness of %ENV elements
260 ok(! exists $ENV{_A_});
261 ok(! exists $ENV{_B_});
264 skip("Can't make list assignment to \%ENV on this system")
265 unless $list_assignment_supported;
266 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
268 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
271 # does implicit localization in foreach skip magic?
275 while (/(o.+?),/gc) {
277 foreach (1..1) { $iter++ }
278 if ($iter > 2) { fail("endless loop"); last; }
283 sub TIESCALAR { bless \my $self, shift }
284 sub FETCH { die "read \$_ forbidden" }
285 sub STORE { die "write \$_ forbidden" }
288 "Nesting" => sub { print '#'; for (1..3) { print }
290 "Reading" => sub { print }, 0,
291 "Matching" => sub { $x = /badness/ }, 0,
292 "Concat" => sub { $_ .= "a" }, 0,
293 "Chop" => sub { chop }, 0,
294 "Filetest" => sub { -x }, 0,
295 "Assignment" => sub { $_ = "Bad" }, 0,
296 # XXX whether next one should fail is debatable
297 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
298 "for local" => sub { for("#ok?\n"){ print } }, 1,
300 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
302 main::ok(($ok xor $@), "Underscore '$name'");
317 # local() and readonly magic variables
319 eval { local $1 = 1 };
320 like($@, qr/Modification of a read-only value attempted/);
322 eval { for ($1) { local $_ = 1 } };
323 like($@, qr/Modification of a read-only value attempted/);
325 # make sure $1 is still read-only
326 eval { for ($1) { local $_ = 1 } };
327 like($@, qr/Modification of a read-only value attempted/);
329 # The s/// adds 'g' magic to $_, but it should remain non-readonly
330 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
333 # Special local() behavior for $[
334 # (see RT #38207 - Useless localization of constant ($[) in getopts.pl}
337 local $TODO = "local() not currently working correctly with \$[";
343 sub f { ok(0 == $[); }
352 no warnings "redefine";
354 local *f1 = sub { "g1" };
355 ::ok(f1() eq "g1", "localised sub via glob");
357 ::ok(f1() eq "f1", "localised sub restored");
359 local $Other::{"f1"} = sub { "h1" };
360 ::ok(f1() eq "h1", "localised sub via stash");
362 ::ok(f1() eq "f1", "localised sub restored");
364 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
365 local $::TODO = "localisation of stash slice not working";
366 ::ok(f1() eq "j1", "localised sub via stash slice");
367 ::ok(f2() eq "j2", "localised sub via stash slice");
370 ::ok(f1() eq "f1", "localised sub restored");
371 ::ok(f2() eq "f2", "localised sub restored");