Front page | perl.perl5.porters |
Postings from February 2003
Re: [perl #21321] local ${"FOO"} does not work
Thread Previous
|
Thread Next
From:
Rafael Garcia-Suarez
Date:
February 22, 2003 15:04
Subject:
Re: [perl #21321] local ${"FOO"} does not work
Message ID:
20030223000327.6f0c11fa.rgarciasuarez@free.fr
Jarkko Hietaniemi (via RT) wrote:
> $ ./perl -e 'local ${"FOO"}=1'
> Can't localize through a reference at -e line 1.
The patch below solves this problem by moving this error message
from compile-time to run-time, and allowing the construct
C<local ${expression()}> as long as the expression() returns a
string, and not a reference.
A priori no edge cases should have slipped in, but some of the twisted
minds here will maybe be able to find holes.
Contains a new test script for this new language feature. I didn't put
the new tests in t/op/local.t because I wanted to use test.pl, that uses
local().
Index: pp.c
===================================================================
--- pp.c (revision 817)
+++ pp.c (working copy)
@@ -211,6 +211,7 @@ PP(pp_rv2gv)
PP(pp_rv2sv)
{
+ GV *gv = Nullgv;
dSP; dTOPss;
if (SvROK(sv)) {
@@ -226,9 +227,9 @@ PP(pp_rv2sv)
}
}
else {
- GV *gv = (GV*)sv;
char *sym;
STRLEN len;
+ gv = (GV*)sv;
if (SvTYPE(gv) != SVt_PVGV) {
if (SvGMAGICAL(sv)) {
@@ -265,8 +266,14 @@ PP(pp_rv2sv)
sv = GvSV(gv);
}
if (PL_op->op_flags & OPf_MOD) {
- if (PL_op->op_private & OPpLVAL_INTRO)
- sv = save_scalar((GV*)TOPs);
+ if (PL_op->op_private & OPpLVAL_INTRO) {
+ if (cUNOP->op_first->op_type == OP_NULL)
+ sv = save_scalar((GV*)TOPs);
+ else if (gv)
+ sv = save_scalar(gv);
+ else
+ Perl_croak(aTHX_ PL_no_localize_ref);
+ }
else if (PL_op->op_private & OPpDEREF)
vivify_ref(sv, PL_op->op_private & OPpDEREF);
}
Index: perl.h
===================================================================
--- perl.h (revision 817)
+++ perl.h (working copy)
@@ -2933,6 +2933,8 @@ EXTCONST char PL_no_func[]
INIT("The %s function is unimplemented");
EXTCONST char PL_no_myglob[]
INIT("\"my\" variable %s can't be in a package");
+EXTCONST char PL_no_localize_ref[]
+ INIT("Can't localize through a reference");
EXTCONST char PL_uuemap[65]
INIT("`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_");
Index: t/op/localref.t
===================================================================
--- t/op/localref.t (working copy)
+++ t/op/localref.t (working copy)
@@ -0,0 +1,85 @@
+#!./perl
+
+chdir 't' if -d 't';
+@INC = qw(. ../lib);
+require "test.pl";
+plan( tests => 63 );
+
+$aa = 1;
+{ local $aa; $aa = 2; is($aa,2); }
+is($aa,1);
+{ local ${aa}; $aa = 3; is($aa,3); }
+is($aa,1);
+{ local ${"aa"}; $aa = 4; is($aa,4); }
+is($aa,1);
+$x = "aa";
+{ local ${$x}; $aa = 5; is($aa,5); undef $x; is($aa,5); }
+is($aa,1);
+$x = "a";
+{ local ${$x x2};$aa = 6; is($aa,6); undef $x; is($aa,6); }
+is($aa,1);
+$x = "aa";
+{ local $$x; $aa = 7; is($aa,7); undef $x; is($aa,7); }
+is($aa,1);
+
+@aa = qw/a b/;
+{ local @aa; @aa = qw/c d/; is("@aa","c d"); }
+is("@aa","a b");
+{ local @{aa}; @aa = qw/e f/; is("@aa","e f"); }
+is("@aa","a b");
+{ local @{"aa"}; @aa = qw/g h/; is("@aa","g h"); }
+is("@aa","a b");
+$x = "aa";
+{ local @{$x}; @aa = qw/i j/; is("@aa","i j"); undef $x; is("@aa","i j"); }
+is("@aa","a b");
+$x = "a";
+{ local @{$x x2};@aa = qw/k l/; is("@aa","k l"); undef $x; is("@aa","k l"); }
+is("@aa","a b");
+$x = "aa";
+{ local @$x; @aa = qw/m n/; is("@aa","m n"); undef $x; is("@aa","m n"); }
+is("@aa","a b");
+
+%aa = qw/a b/;
+{ local %aa; %aa = qw/c d/; is($aa{c},"d"); }
+is($aa{a},"b");
+{ local %{aa}; %aa = qw/e f/; is($aa{e},"f"); }
+is($aa{a},"b");
+{ local %{"aa"}; %aa = qw/g h/; is($aa{g},"h"); }
+is($aa{a},"b");
+$x = "aa";
+{ local %{$x}; %aa = qw/i j/; is($aa{i},"j"); undef $x; is($aa{i},"j"); }
+is($aa{a},"b");
+$x = "a";
+{ local %{$x x2};%aa = qw/k l/; is($aa{k},"l"); undef $x; is($aa{k},"l"); }
+is($aa{a},"b");
+$x = "aa";
+{ local %$x; %aa = qw/m n/; is($aa{m},"n"); undef $x; is($aa{m},"n"); }
+is($aa{a},"b");
+
+sub test_err_localref () {
+ like($@,qr/Can't localize through a reference/,'error');
+}
+$x = \$aa;
+my $y = \$aa;
+eval { local $$x; }; test_err_localref;
+eval { local ${$x}; }; test_err_localref;
+eval { local $$y; }; test_err_localref;
+eval { local ${$y}; }; test_err_localref;
+eval { local ${\$aa}; }; test_err_localref;
+eval { local ${\'aa'}; }; test_err_localref;
+$x = \@aa;
+$y = \@aa;
+eval { local @$x; }; test_err_localref;
+eval { local @{$x}; }; test_err_localref;
+eval { local @$y; }; test_err_localref;
+eval { local @{$y}; }; test_err_localref;
+eval { local @{\@aa}; }; test_err_localref;
+eval { local @{[]}; }; test_err_localref;
+$x = \%aa;
+$y = \%aa;
+eval { local %$x; }; test_err_localref;
+eval { local %{$x}; }; test_err_localref;
+eval { local %$y; }; test_err_localref;
+eval { local %{$y}; }; test_err_localref;
+eval { local %{\%aa}; }; test_err_localref;
+eval { local %{{a=>1}}; };test_err_localref;
Index: t/op/local.t
===================================================================
--- t/op/local.t (revision 817)
+++ t/op/local.t (working copy)
@@ -45,10 +45,10 @@ print $a,@b,@c,%d,$x,$y;
eval 'local($$e)';
print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 21\n";
-eval 'local(@$e)';
+eval '$e = []; local(@$e)';
print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 22\n";
-eval 'local(%$e)';
+eval '$e = {}; local(%$e)';
print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 23\n";
# Array and hash elements
Index: MANIFEST
===================================================================
--- MANIFEST (revision 817)
+++ MANIFEST (working copy)
@@ -2590,6 +2590,7 @@ t/op/lex_assign.t See if ops involving
t/op/lfs.t See if large files work for perlio
t/op/list.t See if array lists work
t/op/local.t See if local works
+t/op/localref.t See if local ${deref} works
t/op/loopctl.t See if next/last/redo work
t/op/lop.t See if logical operators work
t/op/magic.t See if magic variables work
Index: op.c
===================================================================
--- op.c (revision 817)
+++ op.c (working copy)
@@ -1053,8 +1053,6 @@ Perl_mod(pTHX_ OP *o, I32 type)
case OP_RV2AV:
case OP_RV2HV:
- if (!type && cUNOPo->op_first->op_type != OP_GV)
- Perl_croak(aTHX_ "Can't localize through a reference");
if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
PL_modcount = RETURN_UNLIMITED_NUMBER;
return o; /* Treat \(@foo) like ordinary list. */
@@ -1076,8 +1074,6 @@ Perl_mod(pTHX_ OP *o, I32 type)
PL_modcount = RETURN_UNLIMITED_NUMBER;
break;
case OP_RV2SV:
- if (!type && cUNOPo->op_first->op_type != OP_GV)
- Perl_croak(aTHX_ "Can't localize through a reference");
ref(cUNOPo->op_first, o->op_type);
/* FALL THROUGH */
case OP_GV:
Index: pp_hot.c
===================================================================
--- pp_hot.c (revision 817)
+++ pp_hot.c (working copy)
@@ -682,6 +682,9 @@ PP(pp_rv2av)
SETs((SV*)av);
RETURN;
}
+ else if (PL_op->op_flags & OPf_MOD
+ && PL_op->op_private & OPpLVAL_INTRO)
+ Perl_croak(aTHX_ PL_no_localize_ref);
}
else {
if (SvTYPE(sv) == SVt_PVAV) {
@@ -806,6 +809,9 @@ PP(pp_rv2hv)
SETs((SV*)hv);
RETURN;
}
+ else if (PL_op->op_flags & OPf_MOD
+ && PL_op->op_private & OPpLVAL_INTRO)
+ Perl_croak(aTHX_ PL_no_localize_ref);
}
else {
if (SvTYPE(sv) == SVt_PVHV) {
End of Patch.
Thread Previous
|
Thread Next