BEGIN {
chdir 't' if -d 't';
+ @INC = qw(. ../lib);
require './test.pl';
}
-plan tests => 114;
+plan tests => 183;
my $list_assignment_supported = 1;
@a = ('a', 'b', 'c');
{
+ local($a[4]) = 'x';
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+}
+is(scalar(@a), 3);
+ok(!exists $a[3]);
+ok(!exists $a[4]);
+
+@a = ('a', 'b', 'c');
+{
+ local($a[5]) = 'z';
+ $a[4] = 'y';
+ ok(!defined $a[3]);
+ is($a[4], 'y');
+ is($a[5], 'z');
+}
+is(scalar(@a), 5);
+ok(!defined $a[3]);
+is($a[4], 'y');
+ok(!exists $a[5]);
+
+@a = ('a', 'b', 'c');
+{
+ local(@a[4,6]) = ('x', 'z');
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+ ok(!defined $a[5]);
+ is($a[6], 'z');
+}
+is(scalar(@a), 3);
+ok(!exists $a[3]);
+ok(!exists $a[4]);
+ok(!exists $a[5]);
+ok(!exists $a[6]);
+
+@a = ('a', 'b', 'c');
+{
+ local(@a[4,6]) = ('x', 'z');
+ $a[5] = 'y';
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+ is($a[5], 'y');
+ is($a[6], 'z');
+}
+is(scalar(@a), 6);
+ok(!defined $a[3]);
+ok(!defined $a[4]);
+is($a[5], 'y');
+ok(!exists $a[6]);
+
+@a = ('a', 'b', 'c');
+{
local($a[1]) = "X";
shift @a;
}
sub TIEARRAY { bless [], $_[0] }
sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
+ sub EXISTS { print "# EXISTS [@_]\n"; exists $_[0]->[$_[1]]; }
+ sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->[$_[1]]; }
sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
sub FETCHSIZE { scalar(@{$_[0]}) }
sub SHIFT { shift (@{$_[0]}) }
is("@a", $d);
}
+# local() should preserve the existenceness of tied array elements
+@a = ('a', 'b', 'c');
+{
+ local($a[4]) = 'x';
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+}
+is(scalar(@a), 3);
+ok(!exists $a[3]);
+ok(!exists $a[4]);
+
+@a = ('a', 'b', 'c');
+{
+ local($a[5]) = 'z';
+ $a[4] = 'y';
+ ok(!defined $a[3]);
+ is($a[4], 'y');
+ is($a[5], 'z');
+}
+is(scalar(@a), 5);
+ok(!defined $a[3]);
+is($a[4], 'y');
+ok(!exists $a[5]);
+
+@a = ('a', 'b', 'c');
+{
+ local(@a[4,6]) = ('x', 'z');
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+ ok(!defined $a[5]);
+ is($a[6], 'z');
+}
+is(scalar(@a), 3);
+ok(!exists $a[3]);
+ok(!exists $a[4]);
+ok(!exists $a[5]);
+ok(!exists $a[6]);
+
+@a = ('a', 'b', 'c');
+{
+ local(@a[4,6]) = ('x', 'z');
+ $a[5] = 'y';
+ ok(!defined $a[3]);
+ is($a[4], 'x');
+ is($a[5], 'y');
+ is($a[6], 'z');
+}
+is(scalar(@a), 6);
+ok(!defined $a[3]);
+ok(!defined $a[4]);
+is($a[5], 'y');
+ok(!exists $a[6]);
+
+# see if localization works on tied hashes
{
package TH;
sub TIEHASH { bless {}, $_[0] }
sub NEXTKEY { print "# NEXTKEY [@_]\n"; each %{$_[0]} }
}
-# see if localization works on tied hashes
tie %h, 'TH';
%h = ('a' => 1, 'b' => 2, 'c' => 3);
eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
is($@, "");
-# Special local() behavior for $[
-# (see RT #38207 - Useless localization of constant ($[) in getopts.pl}
+# RT #4342 Special local() behavior for $[
{
local $[ = 1;
- local $TODO = "local() not currently working correctly with \$[";
- ok(1 == $[);
- undef $TODO;
+ ok(1 == $[, 'lexcical scope of local $[');
f();
}
}
+# when localising a hash element, the key should be copied, not referenced
+
+{
+ my %h=('k1' => 111);
+ my $k='k1';
+ {
+ local $h{$k}=222;
+
+ is($h{'k1'},222);
+ $k='k2';
+ }
+ ok(! exists($h{'k2'}));
+ is($h{'k1'},111);
+}
+{
+ my %h=('k1' => 111);
+ our $k = 'k1'; # try dynamic too
+ {
+ local $h{$k}=222;
+ is($h{'k1'},222);
+ $k='k2';
+ }
+ ok(! exists($h{'k2'}));
+ is($h{'k1'},111);
+}
+
+like( runperl(stderr => 1,
+ prog => 'use constant foo => q(a);' .
+ 'index(q(a), foo);' .
+ 'local *g=${::}{foo};print q(ok);'), "ok", "[perl #52740]");
+
+# Keep this test last, as it can SEGV
+{
+ local *@;
+ pass("Localised *@");
+ eval {1};
+ pass("Can eval with *@ localised");
+}
+