Re: Untested builtins
[p5sagit/p5-mst-13.2.git] / t / op / local.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl';
6 }
7 plan tests => 85;
8
9 sub foo {
10     local($a, $b) = @_;
11     local($c, $d);
12     $c = "c 3";
13     $d = "d 4";
14     { local($a,$c) = ("a 9", "c 10"); ($x, $y) = ($a, $c); }
15     is($a, "a 1");
16     is($b, "b 2");
17     $c, $d;
18 }
19
20 $a = "a 5";
21 $b = "b 6";
22 $c = "c 7";
23 $d = "d 8";
24
25 my @res;
26 @res =  &foo("a 1","b 2");
27 is($res[0], "c 3");
28 is($res[1], "d 4");
29
30 is($a, "a 5");
31 is($b, "b 6");
32 is($c, "c 7");
33 is($d, "d 8");
34 is($x, "a 9");
35 is($y, "c 10");
36
37 # same thing, only with arrays and associative arrays
38
39 sub foo2 {
40     local($a, @b) = @_;
41     local(@c, %d);
42     @c = "c 3";
43     $d{''} = "d 4";
44     { local($a,@c) = ("a 19", "c 20"); ($x, $y) = ($a, @c); }
45     is($a, "a 1");
46     is("@b", "b 2");
47     $c[0], $d{''};
48 }
49
50 $a = "a 5";
51 @b = "b 6";
52 @c = "c 7";
53 $d{''} = "d 8";
54
55 @res = &foo2("a 1","b 2");
56 is($res[0], "c 3");
57 is($res[1], "d 4");
58
59 is($a, "a 5");
60 is("@b", "b 6");
61 is($c[0], "c 7");
62 is($d{''}, "d 8");
63 is($x, "a 19");
64 is($y, "c 20");
65
66
67 eval 'local($$e)';
68 like($@, qr/Can't localize through a reference/);
69
70 eval '$e = []; local(@$e)';
71 like($@, qr/Can't localize through a reference/);
72
73 eval '$e = {}; local(%$e)';
74 like($@, qr/Can't localize through a reference/);
75
76 # Array and hash elements
77
78 @a = ('a', 'b', 'c');
79 {
80     local($a[1]) = 'foo';
81     local($a[2]) = $a[2];
82     is($a[1], 'foo');
83     is($a[2], 'c');
84     undef @a;
85 }
86 is($a[1], 'b');
87 is($a[2], 'c');
88 ok(!defined $a[0]);
89
90 @a = ('a', 'b', 'c');
91 {
92     local($a[1]) = "X";
93     shift @a;
94 }
95 is($a[0].$a[1], "Xb");
96 {
97     my $d = "@a";
98     local @a = @a;
99     is("@a", $d);
100 }
101
102 %h = ('a' => 1, 'b' => 2, 'c' => 3);
103 {
104     local($h{'a'}) = 'foo';
105     local($h{'b'}) = $h{'b'};
106     is($h{'a'}, 'foo');
107     is($h{'b'}, 2);
108     local($h{'c'});
109     delete $h{'c'};
110 }
111 is($h{'a'}, 1);
112 is($h{'b'}, 2);
113 {
114     my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
115     local %h = %h;
116     is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
117 }
118 is($h{'c'}, 3);
119
120 # check for scope leakage
121 $a = 'outer';
122 if (1) { local $a = 'inner' }
123 is($a, 'outer');
124
125 # see if localization works when scope unwinds
126 local $m = 5;
127 eval {
128     for $m (6) {
129         local $m = 7;
130         die "bye";
131     }
132 };
133 is($m, 5);
134
135 # see if localization works on tied arrays
136 {
137     package TA;
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]}) }
144     sub EXTEND {}
145 }
146
147 tie @a, 'TA';
148 @a = ('a', 'b', 'c');
149 {
150     local($a[1]) = 'foo';
151     local($a[2]) = $a[2];
152     is($a[1], 'foo');
153     is($a[2], 'c');
154     @a = ();
155 }
156 is($a[1], 'b');
157 is($a[2], 'c');
158 ok(!defined $a[0]);
159 {
160     my $d = "@a";
161     local @a = @a;
162     is("@a", $d);
163 }
164
165 {
166     package TH;
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]} }
175 }
176
177 # see if localization works on tied hashes
178 tie %h, 'TH';
179 %h = ('a' => 1, 'b' => 2, 'c' => 3);
180
181 {
182     local($h{'a'}) = 'foo';
183     local($h{'b'}) = $h{'b'};
184     local($h{'y'});
185     local($h{'z'}) = 33;
186     is($h{'a'}, 'foo');
187     is($h{'b'}, 2);
188     local($h{'c'});
189     delete $h{'c'};
190 }
191 is($h{'a'}, 1);
192 is($h{'b'}, 2);
193 is($h{'c'}, 3);
194 # local() should preserve the existenceness of tied hash elements
195 ok(! exists $h{'y'});
196 ok(! exists $h{'z'});
197 TODO: {
198     todo_skip("Localize entire tied hash");
199     my $d = join("\n", map { "$_=>$h{$_}" } sort keys %h);
200     local %h = %h;
201     is(join("\n", map { "$_=>$h{$_}" } sort keys %h), $d);
202 }
203
204 @a = ('a', 'b', 'c');
205 {
206     local($a[1]) = "X";
207     shift @a;
208 }
209 is($a[0].$a[1], "Xb");
210
211 # now try the same for %SIG
212
213 $SIG{TERM} = 'foo';
214 $SIG{INT} = \&foo;
215 $SIG{__WARN__} = $SIG{INT};
216 {
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);
223     local($SIG{INT});
224     delete $SIG{__WARN__};
225 }
226 is($SIG{TERM}, 'main::foo');
227 is($SIG{INT}, \&foo);
228 is($SIG{__WARN__}, \&foo);
229 {
230     my $d = join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG);
231     local %SIG = %SIG;
232     is(join("\n", map { "$_=>$SIG{$_}" } sort keys %SIG), $d);
233 }
234
235 # and for %ENV
236
237 $ENV{_X_} = 'a';
238 $ENV{_Y_} = 'b';
239 $ENV{_Z_} = 'c';
240 {
241     local($ENV{_A_});
242     local($ENV{_B_}) = 'foo';
243     local($ENV{_X_}) = 'foo';
244     local($ENV{_Y_}) = $ENV{_Y_};
245     is($ENV{_X_}, 'foo');
246     is($ENV{_Y_}, 'b');
247     local($ENV{_Z_});
248     delete $ENV{_Z_};
249 }
250 is($ENV{_X_}, 'a');
251 is($ENV{_Y_}, 'b');
252 is($ENV{_Z_}, 'c');
253 # local() should preserve the existenceness of %ENV elements
254 ok(! exists $ENV{_A_});
255 ok(! exists $ENV{_B_});
256 {
257     my $d = join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV);
258     local %ENV = %ENV;
259     is(join("\n", map { "$_=>$ENV{$_}" } sort keys %ENV), $d);
260 }
261
262 # does implicit localization in foreach skip magic?
263
264 $_ = "o 0,o 1,";
265 my $iter = 0;
266 while (/(o.+?),/gc) {
267     is($1, "o $iter");
268     foreach (1..1) { $iter++ }
269     if ($iter > 2) { fail("endless loop"); last; }
270 }
271
272 {
273     package UnderScore;
274     sub TIESCALAR { bless \my $self, shift }
275     sub FETCH { die "read  \$_ forbidden" }
276     sub STORE { die "write \$_ forbidden" }
277     tie $_, __PACKAGE__;
278     my @tests = (
279         "Nesting"     => sub { print '#'; for (1..3) { print }
280                                print "\n" },                    1,
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,
290     );
291     while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
292         eval { &$code };
293         main::ok(($ok xor $@), "Underscore '$name'");
294     }
295     untie $_;
296 }
297
298 {
299     # BUG 20001205.22
300     my %x;
301     $x{a} = 1;
302     { local $x{b} = 1; }
303     ok(! exists $x{b});
304     { local @x{c,d,e}; }
305     ok(! exists $x{c});
306 }
307
308 # local() and readonly magic variables
309
310 eval { local $1 = 1 };
311 like($@, qr/Modification of a read-only value attempted/);
312
313 eval { for ($1) { local $_ = 1 } };
314 like($@, qr/Modification of a read-only value attempted/);
315
316 # make sure $1 is still read-only
317 eval { for ($1) { local $_ = 1 } };
318 like($@, qr/Modification of a read-only value attempted/);
319
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/ } } };
322 is($@, "");