Improve and restructure t/op/pat.t and split out some unicode related tests into...
[p5sagit/p5-mst-13.2.git] / t / op / local.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(. ../lib);
6     require './test.pl';
7 }
8 plan tests => 123;
9
10 my $list_assignment_supported = 1;
11
12 #mg.c says list assignment not supported on VMS, EPOC, and SYMBIAN.
13 $list_assignment_supported = 0 if ($^O eq 'VMS');
14
15
16 sub foo {
17     local($a, $b) = @_;
18     local($c, $d);
19     $c = "c 3";
20     $d = "d 4";
21     { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
22     is($a, "a 1");
23     is($b, "b 2");
24     $c, $d;
25 }
26
27 $a = "a 5";
28 $b = "b 6";
29 $c = "c 7";
30 $d = "d 8";
31
32 my @res;
33 @res =  &foo("a 1","b 2");
34 is($res[0], "c 3");
35 is($res[1], "d 4");
36
37 is($a, "a 5");
38 is($b, "b 6");
39 is($c, "c 7");
40 is($d, "d 8");
41 is($x, "a 9");
42 is($y, "c 10");
43
44 # same thing, only with arrays and associative arrays
45
46 sub foo2 {
47     local($a, @b) = @_;
48     local(@c, %d);
49     @c = "c 3";
50     $d{''} = "d 4";
51     { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
52     is($a, "a 1");
53     is("@b", "b 2");
54     $c[0], $d{''};
55 }
56
57 $a = "a 5";
58 @b = "b 6";
59 @c = "c 7";
60 $d{''} = "d 8";
61
62 @res = &foo2("a 1","b 2");
63 is($res[0], "c 3");
64 is($res[1], "d 4");
65
66 is($a, "a 5");
67 is("@b", "b 6");
68 is($c[0], "c 7");
69 is($d{''}, "d 8");
70 is($x, "a 19");
71 is($y, "c 20");
72
73
74 eval 'local($$e)';
75 like($@, qr/Can't localize through a reference/);
76
77 eval '$e = []; local(@$e)';
78 like($@, qr/Can't localize through a reference/);
79
80 eval '$e = {}; local(%$e)';
81 like($@, qr/Can't localize through a reference/);
82
83 # Array and hash elements
84
85 @a = ('a', 'b', 'c');
86 {
87     local($a[1]) = 'foo';
88     local($a[2]) = $a[2];
89     is($a[1], 'foo');
90     is($a[2], 'c');
91     undef @a;
92 }
93 is($a[1], 'b');
94 is($a[2], 'c');
95 ok(!defined $a[0]);
96
97 @a = ('a', 'b', 'c');
98 {
99     local($a[1]) = "X";
100     shift @a;
101 }
102 is($a[0].$a[1], "Xb");
103 {
104     my $d = "@a";
105     local @a = @a;
106     is("@a", $d);
107 }
108
109 %h = ('a' => 1, 'b' => 2, 'c' => 3);
110 {
111     local($h{'a'}) = 'foo';
112     local($h{'b'}) = $h{'b'};
113     is($h{'a'}, 'foo');
114     is($h{'b'}, 2);
115     local($h{'c'});
116     delete $h{'c'};
117 }
118 is($h{'a'}, 1);
119 is($h{'b'}, 2);
120 {
121     my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
122     local %h = %h;
123     is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
124 }
125 is($h{'c'}, 3);
126
127 # check for scope leakage
128 $a = 'outer';
129 if (1) { local $a = 'inner' }
130 is($a, 'outer');
131
132 # see if localization works when scope unwinds
133 local $m = 5;
134 eval {
135     for $m (6) {
136         local $m = 7;
137         die "bye";
138     }
139 };
140 is($m, 5);
141
142 # see if localization works on tied arrays
143 {
144     package TA;
145     sub TIEARRAY { bless [], $_[0] }
146     sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
147     sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
148     sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
149     sub FETCHSIZE { scalar(@{$_[0]}) }
150     sub SHIFT { shift (@{$_[0]}) }
151     sub EXTEND {}
152 }
153
154 tie @a, 'TA';
155 @a = ('a', 'b', 'c');
156 {
157     local($a[1]) = 'foo';
158     local($a[2]) = $a[2];
159     is($a[1], 'foo');
160     is($a[2], 'c');
161     @a = ();
162 }
163 is($a[1], 'b');
164 is($a[2], 'c');
165 ok(!defined $a[0]);
166 {
167     my $d = "@a";
168     local @a = @a;
169     is("@a", $d);
170 }
171
172 {
173     package TH;
174     sub TIEHASH { bless {}, $_[0] }
175     sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
176     sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
177     sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
178     sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
179     sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
180     sub FIRSTKEY { print "# FIRSTKEY [@_]\n"; keys %{$_[0]}; each %{$_[0]} }
181     sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
182 }
183
184 # see if localization works on tied hashes
185 tie %h, 'TH';
186 %h = ('a' => 1, 'b' => 2, 'c' => 3);
187
188 {
189     local($h{'a'}) = 'foo';
190     local($h{'b'}) = $h{'b'};
191     local($h{'y'});
192     local($h{'z'}) = 33;
193     is($h{'a'}, 'foo');
194     is($h{'b'}, 2);
195     local($h{'c'});
196     delete $h{'c'};
197 }
198 is($h{'a'}, 1);
199 is($h{'b'}, 2);
200 is($h{'c'}, 3);
201 # local() should preserve the existenceness of tied hash elements
202 ok(! exists $h{'y'});
203 ok(! exists $h{'z'});
204 TODO: {
205     todo_skip("Localize entire tied hash");
206     my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
207     local %h = %h;
208     is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
209 }
210
211 @a = ('a', 'b', 'c');
212 {
213     local($a[1]) = "X";
214     shift @a;
215 }
216 is($a[0].$a[1], "Xb");
217
218 # now try the same for %SIG
219
220 $SIG{TERM} = 'foo';
221 $SIG{INT} = \&foo;
222 $SIG{__WARN__} = $SIG{INT};
223 {
224     local($SIG{TERM}) = $SIG{TERM};
225     local($SIG{INT}) = $SIG{INT};
226     local($SIG{__WARN__}) = $SIG{__WARN__};
227     is($SIG{TERM}, 'main::foo');
228     is($SIG{INT}, \&foo);
229     is($SIG{__WARN__}, \&foo);
230     local($SIG{INT});
231     delete $SIG{__WARN__};
232 }
233 is($SIG{TERM}, 'main::foo');
234 is($SIG{INT}, \&foo);
235 is($SIG{__WARN__}, \&foo);
236 {
237     my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
238     local %SIG = %SIG;
239     is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
240 }
241
242 # and for %ENV
243
244 $ENV{_X_} = 'a';
245 $ENV{_Y_} = 'b';
246 $ENV{_Z_} = 'c';
247 {
248     local($ENV{_A_});
249     local($ENV{_B_}) = 'foo';
250     local($ENV{_X_}) = 'foo';
251     local($ENV{_Y_}) = $ENV{_Y_};
252     is($ENV{_X_}, 'foo');
253     is($ENV{_Y_}, 'b');
254     local($ENV{_Z_});
255     delete $ENV{_Z_};
256 }
257 is($ENV{_X_}, 'a');
258 is($ENV{_Y_}, 'b');
259 is($ENV{_Z_}, 'c');
260 # local() should preserve the existenceness of %ENV elements
261 ok(! exists $ENV{_A_});
262 ok(! exists $ENV{_B_});
263
264 SKIP: {
265     skip("Can't make list assignment to \%ENV on this system")
266         unless $list_assignment_supported;
267     my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
268     local %ENV = %ENV;
269     is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
270 }
271
272 # does implicit localization in foreach skip magic?
273
274 $_ = "o 0,o 1,";
275 my $iter = 0;
276 while (/(o.+?),/gc) {
277     is($1, "o $iter");
278     foreach (1..1) { $iter++ }
279     if ($iter > 2) { fail("endless loop"); last; }
280 }
281
282 {
283     package UnderScore;
284     sub TIESCALAR { bless \my $self, shift }
285     sub FETCH { die "read  \$_ forbidden" }
286     sub STORE { die "write \$_ forbidden" }
287     tie $_, __PACKAGE__;
288     my @tests = (
289         "Nesting"     => sub { print '#'; for (1..3) { print }
290                                print "\n" },                    1,
291         "Reading"     => sub { print },                         0,
292         "Matching"    => sub { $x = /badness/ },                0,
293         "Concat"      => sub { $_ .= "a" },                     0,
294         "Chop"        => sub { chop },                          0,
295         "Filetest"    => sub { -x },                            0,
296         "Assignment"  => sub { $_ = "Bad" },                    0,
297         # XXX whether next one should fail is debatable
298         "Local \$_"   => sub { local $_  = 'ok?'; print },      0,
299         "for local"   => sub { for("#ok?\n"){ print } },        1,
300     );
301     while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
302         eval { &$code };
303         main::ok(($ok xor $@), "Underscore '$name'");
304     }
305     untie $_;
306 }
307
308 {
309     # BUG 20001205.22
310     my %x;
311     $x{a} = 1;
312     { local $x{b} = 1; }
313     ok(! exists $x{b});
314     { local @x{c,d,e}; }
315     ok(! exists $x{c});
316 }
317
318 # local() and readonly magic variables
319
320 eval { local $1 = 1 };
321 like($@, qr/Modification of a read-only value attempted/);
322
323 eval { for ($1) { local $_ = 1 } };
324 like($@, qr/Modification of a read-only value attempted/);
325
326 # make sure $1 is still read-only
327 eval { for ($1) { local $_ = 1 } };
328 like($@, qr/Modification of a read-only value attempted/);
329
330 # The s/// adds 'g' magic to $_, but it should remain non-readonly
331 eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
332 is($@, "");
333
334 # RT #4342 Special local() behavior for $[
335 {
336     local $[ = 1;
337     ok(1 == $[, 'lexcical scope of local $[');
338     f();
339 }
340
341 sub f { ok(0 == $[); }
342
343 # sub localisation
344 {
345         package Other;
346
347         sub f1 { "f1" }
348         sub f2 { "f2" }
349
350         no warnings "redefine";
351         {
352                 local *f1 = sub  { "g1" };
353                 ::ok(f1() eq "g1", "localised sub via glob");
354         }
355         ::ok(f1() eq "f1", "localised sub restored");
356         {
357                 local $Other::{"f1"} = sub { "h1" };
358                 ::ok(f1() eq "h1", "localised sub via stash");
359         }
360         ::ok(f1() eq "f1", "localised sub restored");
361         {
362                 local @Other::{qw/ f1 f2 /} = (sub { "j1" }, sub { "j2" });
363                 ::ok(f1() eq "j1", "localised sub via stash slice");
364                 ::ok(f2() eq "j2", "localised sub via stash slice");
365         }
366         ::ok(f1() eq "f1", "localised sub restored");
367         ::ok(f2() eq "f2", "localised sub restored");
368 }
369
370 # Localising unicode keys (bug #38815)
371 {
372     my %h;
373     $h{"\243"} = "pound";
374     $h{"\302\240"} = "octects";
375     is(scalar keys %h, 2);
376     {
377         my $unicode = chr 256;
378         my $ambigous = "\240" . $unicode;
379         chop $ambigous;
380         local $h{$unicode} = 256;
381         local $h{$ambigous} = 160;
382
383         is(scalar keys %h, 4);
384         is($h{"\243"}, "pound");
385         is($h{$unicode}, 256);
386         is($h{$ambigous}, 160);
387         is($h{"\302\240"}, "octects");
388     }
389     is(scalar keys %h, 2);
390     is($h{"\243"}, "pound");
391     is($h{"\302\240"}, "octects");
392 }
393
394 # And with slices
395 {
396     my %h;
397     $h{"\243"} = "pound";
398     $h{"\302\240"} = "octects";
399     is(scalar keys %h, 2);
400     {
401         my $unicode = chr 256;
402         my $ambigous = "\240" . $unicode;
403         chop $ambigous;
404         local @h{$unicode, $ambigous} = (256, 160);
405
406         is(scalar keys %h, 4);
407         is($h{"\243"}, "pound");
408         is($h{$unicode}, 256);
409         is($h{$ambigous}, 160);
410         is($h{"\302\240"}, "octects");
411     }
412     is(scalar keys %h, 2);
413     is($h{"\243"}, "pound");
414     is($h{"\302\240"}, "octects");
415 }
416
417 # [perl #39012] localizing @_ element then shifting frees element too # soon
418
419 {
420     my $x;
421     my $y = bless [], 'X39012';
422     sub X39012::DESTROY { $x++ }
423     sub { local $_[0]; shift }->($y);
424     ok(!$x,  '[perl #39012]');
425     
426 }
427
428 # when localising a hash element, the key should be copied, not referenced
429
430 {
431     my %h=('k1' => 111);
432     my $k='k1';
433     {
434         local $h{$k}=222;
435
436         is($h{'k1'},222);
437         $k='k2';
438     }
439     ok(! exists($h{'k2'}));
440     is($h{'k1'},111);
441 }
442 {
443     my %h=('k1' => 111);
444     our $k = 'k1';  # try dynamic too
445     {
446         local $h{$k}=222;
447         is($h{'k1'},222);
448         $k='k2';
449     }
450     ok(! exists($h{'k2'}));
451     is($h{'k1'},111);
452 }
453
454 like( runperl(stderr => 1,
455               prog => 'use constant foo => q(a);' .
456                       'index(q(a), foo);' .
457                       'local *g=${::}{foo};print q(ok);'), "ok", "[perl #52740]");
458
459 # Keep this test last, as it can SEGV
460 {
461     local *@;
462     pass("Localised *@");
463     eval {1};
464     pass("Can eval with *@ localised");
465 }
466