10 { local($a,$c) = ("ok 9\n", "ok 10\n"); ($x, $y) = ($a, $c); }
20 print &foo("ok 1\n","ok 2\n");
22 print $a,$b,$c,$d,$x,$y;
24 # same thing, only with arrays and associative arrays
31 { local($a,@c) = ("ok 19\n", "ok 20\n"); ($x, $y) = ($a, @c); }
41 print &foo2("ok 11\n","ok 12\n");
43 print $a,@b,@c,%d,$x,$y;
46 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 21\n";
49 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 22\n";
52 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 23\n";
54 # Array and hash elements
60 print +($a[1] eq 'foo') ? "" : "not ", "ok 24\n";
61 print +($a[2] eq 'c') ? "" : "not ", "ok 25\n";
64 print +($a[1] eq 'b') ? "" : "not ", "ok 26\n";
65 print +($a[2] eq 'c') ? "" : "not ", "ok 27\n";
66 print +(!defined $a[0]) ? "" : "not ", "ok 28\n";
73 print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 29\n";
75 %h = ('a' => 1, 'b' => 2, 'c' => 3);
77 local($h{'a'}) = 'foo';
78 local($h{'b'}) = $h{'b'};
79 print +($h{'a'} eq 'foo') ? "" : "not ", "ok 30\n";
80 print +($h{'b'} == 2) ? "" : "not ", "ok 31\n";
84 print +($h{'a'} == 1) ? "" : "not ", "ok 32\n";
85 print +($h{'b'} == 2) ? "" : "not ", "ok 33\n";
86 print +($h{'c'} == 3) ? "" : "not ", "ok 34\n";
88 # check for scope leakage
90 if (1) { local $a = 'inner' }
91 print +($a eq 'outer') ? "" : "not ", "ok 35\n";
93 # see if localization works when scope unwinds
101 print $m == 5 ? "" : "not ", "ok 36\n";
103 # see if localization works on tied arrays
106 sub TIEARRAY { bless [], $_[0] }
107 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
108 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
109 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
110 sub FETCHSIZE { scalar(@{$_[0]}) }
111 sub SHIFT { shift (@{$_[0]}) }
116 @a = ('a', 'b', 'c');
118 local($a[1]) = 'foo';
119 local($a[2]) = $a[2];
120 print +($a[1] eq 'foo') ? "" : "not ", "ok 37\n";
121 print +($a[2] eq 'c') ? "" : "not ", "ok 38\n";
124 print +($a[1] eq 'b') ? "" : "not ", "ok 39\n";
125 print +($a[2] eq 'c') ? "" : "not ", "ok 40\n";
126 print +(!defined $a[0]) ? "" : "not ", "ok 41\n";
130 sub TIEHASH { bless {}, $_[0] }
131 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
132 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
133 sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->{$_[1]}; }
134 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
135 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
138 # see if localization works on tied hashes
140 %h = ('a' => 1, 'b' => 2, 'c' => 3);
143 local($h{'a'}) = 'foo';
144 local($h{'b'}) = $h{'b'};
145 print +($h{'a'} eq 'foo') ? "" : "not ", "ok 42\n";
146 print +($h{'b'} == 2) ? "" : "not ", "ok 43\n";
150 print +($h{'a'} == 1) ? "" : "not ", "ok 44\n";
151 print +($h{'b'} == 2) ? "" : "not ", "ok 45\n";
152 print +($h{'c'} == 3) ? "" : "not ", "ok 46\n";
154 @a = ('a', 'b', 'c');
159 print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 47\n";
161 # now try the same for %SIG
165 $SIG{__WARN__} = $SIG{INT};
167 local($SIG{TERM}) = $SIG{TERM};
168 local($SIG{INT}) = $SIG{INT};
169 local($SIG{__WARN__}) = $SIG{__WARN__};
170 print +($SIG{TERM} eq 'main::foo') ? "" : "not ", "ok 48\n";
171 print +($SIG{INT} eq \&foo) ? "" : "not ", "ok 49\n";
172 print +($SIG{__WARN__} eq \&foo) ? "" : "not ", "ok 50\n";
174 delete $SIG{__WARN__};
176 print +($SIG{TERM} eq 'main::foo') ? "" : "not ", "ok 51\n";
177 print +($SIG{INT} eq \&foo) ? "" : "not ", "ok 52\n";
178 print +($SIG{__WARN__} eq \&foo) ? "" : "not ", "ok 53\n";
186 local($ENV{_X_}) = 'foo';
187 local($ENV{_Y_}) = $ENV{_Y_};
188 print +($ENV{_X_} eq 'foo') ? "" : "not ", "ok 54\n";
189 print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 55\n";
193 print +($ENV{_X_} eq 'a') ? "" : "not ", "ok 56\n";
194 print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 57\n";
195 print +($ENV{_Z_} eq 'c') ? "" : "not ", "ok 58\n";
197 # does implicit localization in foreach skip magic?
201 while (/(o.+?),/gc) {
203 foreach (1..1) { $iter++ }
204 if ($iter > 2) { print "not ok 60\n"; last; }
209 sub TIESCALAR { bless \my $self, shift }
210 sub FETCH { die "read \$_ forbidden" }
211 sub STORE { die "write \$_ forbidden" }
215 "Nesting" => sub { print '#'; for (1..3) { print }
217 "Reading" => sub { print }, 0,
218 "Matching" => sub { $x = /badness/ }, 0,
219 "Concat" => sub { $_ .= "a" }, 0,
220 "Chop" => sub { chop }, 0,
221 "Filetest" => sub { -x }, 0,
222 "Assignment" => sub { $_ = "Bad" }, 0,
223 # XXX whether next one should fail is debatable
224 "Local \$_" => sub { local $_ = 'ok?'; print }, 0,
225 "for local" => sub { for("#ok?\n"){ print } }, 1,
227 while ( ($name, $code, $ok) = splice(@tests, 0, 3) ) {
228 print "# Testing $name\n";
230 print(($ok xor $@) ? "ok $t\n" : "not ok $t\n");
241 print "not " if exists $x{b};
244 print "not " if exists $x{c};