3 # $RCSfile: local.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:04 $
7 # XXX known to leak scalars
8 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
15 { local($a,$c) = ("ok 9\n", "ok 10\n"); ($x, $y) = ($a, $c); }
25 print &foo("ok 1\n","ok 2\n");
27 print $a,$b,$c,$d,$x,$y;
29 # same thing, only with arrays and associative arrays
36 { local($a,@c) = ("ok 19\n", "ok 20\n"); ($x, $y) = ($a, @c); }
46 print &foo2("ok 11\n","ok 12\n");
48 print $a,@b,@c,%d,$x,$y;
51 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 21\n";
54 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 22\n";
57 print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 23\n";
59 # Array and hash elements
65 print +($a[1] eq 'foo') ? "" : "not ", "ok 24\n";
66 print +($a[2] eq 'c') ? "" : "not ", "ok 25\n";
69 print +($a[1] eq 'b') ? "" : "not ", "ok 26\n";
70 print +($a[2] eq 'c') ? "" : "not ", "ok 27\n";
71 print +(!defined $a[0]) ? "" : "not ", "ok 28\n";
78 print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 29\n";
80 %h = ('a' => 1, 'b' => 2, 'c' => 3);
82 local($h{'a'}) = 'foo';
83 local($h{'b'}) = $h{'b'};
84 print +($h{'a'} eq 'foo') ? "" : "not ", "ok 30\n";
85 print +($h{'b'} == 2) ? "" : "not ", "ok 31\n";
89 print +($h{'a'} == 1) ? "" : "not ", "ok 32\n";
90 print +($h{'b'} == 2) ? "" : "not ", "ok 33\n";
91 print +($h{'c'} == 3) ? "" : "not ", "ok 34\n";
93 # check for scope leakage
95 if (1) { local $a = 'inner' }
96 print +($a eq 'outer') ? "" : "not ", "ok 35\n";
98 # see if localization works when scope unwinds
106 print $m == 5 ? "" : "not ", "ok 36\n";
108 # see if localization works on tied arrays
111 sub TIEARRAY { bless [], $_[0] }
112 sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
113 sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
114 sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
115 sub FETCHSIZE { scalar(@{$_[0]}) }
116 sub SHIFT { shift (@{$_[0]}) }
121 @a = ('a', 'b', 'c');
123 local($a[1]) = 'foo';
124 local($a[2]) = $a[2];
125 print +($a[1] eq 'foo') ? "" : "not ", "ok 37\n";
126 print +($a[2] eq 'c') ? "" : "not ", "ok 38\n";
129 print +($a[1] eq 'b') ? "" : "not ", "ok 39\n";
130 print +($a[2] eq 'c') ? "" : "not ", "ok 40\n";
131 print +(!defined $a[0]) ? "" : "not ", "ok 41\n";
135 sub TIEHASH { bless {}, $_[0] }
136 sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
137 sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
138 sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
139 sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
142 # see if localization works on tied hashes
144 %h = ('a' => 1, 'b' => 2, 'c' => 3);
147 local($h{'a'}) = 'foo';
148 local($h{'b'}) = $h{'b'};
149 print +($h{'a'} eq 'foo') ? "" : "not ", "ok 42\n";
150 print +($h{'b'} == 2) ? "" : "not ", "ok 43\n";
154 print +($h{'a'} == 1) ? "" : "not ", "ok 44\n";
155 print +($h{'b'} == 2) ? "" : "not ", "ok 45\n";
156 print +($h{'c'} == 3) ? "" : "not ", "ok 46\n";
158 @a = ('a', 'b', 'c');
163 print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 47\n";
165 # now try the same for %SIG
169 $SIG{__WARN__} = $SIG{INT};
171 local($SIG{TERM}) = $SIG{TERM};
172 local($SIG{INT}) = $SIG{INT};
173 local($SIG{__WARN__}) = $SIG{__WARN__};
174 print +($SIG{TERM} eq 'main::foo') ? "" : "not ", "ok 48\n";
175 print +($SIG{INT} eq \&foo) ? "" : "not ", "ok 49\n";
176 print +($SIG{__WARN__} eq \&foo) ? "" : "not ", "ok 50\n";
178 delete $SIG{__WARN__};
180 print +($SIG{TERM} eq 'main::foo') ? "" : "not ", "ok 51\n";
181 print +($SIG{INT} eq \&foo) ? "" : "not ", "ok 52\n";
182 print +($SIG{__WARN__} eq \&foo) ? "" : "not ", "ok 53\n";
190 local($ENV{_X_}) = 'foo';
191 local($ENV{_Y_}) = $ENV{_Y_};
192 print +($ENV{_X_} eq 'foo') ? "" : "not ", "ok 54\n";
193 print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 55\n";
197 print +($ENV{_X_} eq 'a') ? "" : "not ", "ok 56\n";
198 print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 57\n";
199 print +($ENV{_Z_} eq 'c') ? "" : "not ", "ok 58\n";