14 { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
26 @res = &foo("a 1","b 2");
37 # same thing, only with arrays and associative arrays
44 { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
55 @res = &foo2("a 1","b 2");
68 like($@, qr/Can't localize through a reference/);
70 eval '$e = []; local(@$e)';
71 like($@, qr/Can't localize through a reference/);
73 eval '$e = {}; local(%$e)';
74 like($@, qr/Can't localize through a reference/);
76 # Array and hash elements
95 is($a[0].$a[1], "Xb");
102 %h = ('a' => 1, 'b' => 2, 'c' => 3);
104 local($h{'a'}) = 'foo';
105 local($h{'b'}) = $h{'b'};
114 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
116 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
120 # check for scope leakage
122 if (1) { local $a = 'inner' }
125 # see if localization works when scope unwinds
135 # see if localization works on tied arrays
138 sub TIEARRAY { bless [], $_[0] }
139 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
140 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
141 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
142 sub FETCHSIZE { scalar(@{$_[0]}) }
143 sub SHIFT { shift (@{$_[0]}) }
148 @a = ('a', 'b', 'c');
150 local($a[1]) = 'foo';
151 local($a[2]) = $a[2];
167 sub TIEHASH { bless {}, $_[0] }
168 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
169 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
170 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
171 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
172 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
173 sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
174 sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
177 # see if localization works on tied hashes
179 %h = ('a' => 1, 'b' => 2, 'c' => 3);
182 local($h{'a'}) = 'foo';
183 local($h{'b'}) = $h{'b'};
194 # local() should preserve the existenceness of tied hash elements
195 ok(! exists $h{'y'});
196 ok(! exists $h{'z'});
198 todo_skip("Localize entire tied hash");
199 my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
201 is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
204 @a = ('a', 'b', 'c');
209 is($a[0].$a[1], "Xb");
211 # now try the same for %SIG
215 $SIG{__WARN__} = $SIG{INT};
217 local($SIG{TERM}) = $SIG{TERM};
218 local($SIG{INT}) = $SIG{INT};
219 local($SIG{__WARN__}) = $SIG{__WARN__};
220 is($SIG{TERM}, 'main::foo');
221 is($SIG{INT}, \&foo);
222 is($SIG{__WARN__}, \&foo);
224 delete $SIG{__WARN__};
226 is($SIG{TERM}, 'main::foo');
227 is($SIG{INT}, \&foo);
228 is($SIG{__WARN__}, \&foo);
230 my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
232 is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
242 local($ENV{_B_}) = 'foo';
243 local($ENV{_X_}) = 'foo';
244 local($ENV{_Y_}) = $ENV{_Y_};
245 is($ENV{_X_}, 'foo');
253 # local() should preserve the existenceness of %ENV elements
254 ok(! exists $ENV{_A_});
255 ok(! exists $ENV{_B_});
257 my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
259 is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
262 # does implicit localization in foreach skip magic?
266 while (/(o.+?),/gc) {
268 foreach (1..1) { $iter++ }
269 if ($iter > 2) { fail("endless loop"); last; }
274 sub TIESCALAR { bless \my $self, shift }
275 sub FETCH { die "read \$_ forbidden" }
276 sub STORE { die "write \$_ forbidden" }
279 "Nesting" => sub { print '#'; for (1..3) { print }
281 "Reading" => sub { print }, 0,
282 "Matching" => sub { $x = /badness/ }, 0,
283 "Concat" => sub { $_ .= "a" }, 0,
284 "Chop" => sub { chop }, 0,
285 "Filetest" => sub { -x }, 0,
286 "Assignment" => sub { $_ = "Bad" }, 0,
287 # XXX whether next one should fail is debatable
288 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
289 "for local" => sub { for("#ok?\n"){ print } }, 1,
291 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
293 main::ok(($ok xor $@), "Underscore '$name'");
308 # local() and readonly magic variables
310 eval { local $1 = 1 };
311 like($@, qr/Modification of a read-only value attempted/);
313 eval { for ($1) { local $_ = 1 } };
314 like($@, qr/Modification of a read-only value attempted/);
316 # make sure $1 is still read-only
317 eval { for ($1) { local $_ = 1 } };
318 like($@, qr/Modification of a read-only value attempted/);
320 # The s/// adds 'g' magic to $_, but it should remain non-readonly
321 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };