Front page | perl.perl5.porters |
Postings from March 2011
Re: [perl #84774] local $_ calls STORE when $_ is aliased to a tiedhash element
Thread Previous
|
Thread Next
From:
demerphq
Date:
March 18, 2011 03:30
Subject:
Re: [perl #84774] local $_ calls STORE when $_ is aliased to a tiedhash element
Message ID:
AANLkTim3Yc9Mdnmcvv3LKuZ=zsG1nS-KXpYY++rtNrxt@mail.gmail.com
On 18 March 2011 08:22, Jan Dubois <jand@activestate.com> wrote:
> On Sun, 27 Feb 2011, Aristotle Pagaltzis wrote:
>> * Jan Dubois <jand@activestate.com> [2011-02-25 06:25]:
>> > An alternative would be to only treat "local $_" specially. This
>> > could be done at compile time, so wouldn't need any SV flags at
>> > runtime.
>>
>> I vote for this. `$_` represents an unnamed scalar, it refers to the
>> implicit topic of a section of code, whereas all other variables are
>> explicit names of particular values.
>
> I have implemented this (at runtime, not compiletime) with the attached
> patch. "local $_" will strip *all* types of magic from $_, not just
> tie() magic, so it doesn't matter what $_ has been aliased too, it is
> always safe to localize it. It does not affect localization of any
> other scalars, so File::chdir is unaffected.
>
> It obviously needs additional tests, and changes to perlsub.pod, but
> I would first like to get a ruling from Jesse if this is acceptable
> for blead, and ideally a review from Dave if the patch could have
> any other side-effects that I have overlooked.
>
> Cheers,
> -Jan
This isnt an ack on the code, but +1 on the idea. Thanks Jan!
>
> diff --git a/mg.c b/mg.c
> index 7b5fdf5..85a1a2b 100644
> --- a/mg.c
> +++ b/mg.c
> @@ -286,7 +286,7 @@ Perl_mg_set(pTHX_ SV *sv)
> mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
> (SSPTR(mgs_ix, MGS*))->mgs_magical = 0;
> }
> - if (PL_localizing == 2 && !S_is_container_magic(mg))
> + if (PL_localizing == 2 && (!S_is_container_magic(mg) || sv == DEFSV))
> continue;
> if (vtbl && vtbl->svt_set)
> vtbl->svt_set(aTHX_ sv, mg);
> @@ -511,6 +511,9 @@ Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
>
> PERL_ARGS_ASSERT_MG_LOCALIZE;
>
> + if (nsv == DEFSV)
> + return;
> +
> for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
> const MGVTBL* const vtbl = mg->mg_virtual;
> if (!S_is_container_magic(mg))
> diff --git a/t/op/local.t b/t/op/local.t
> index f664df4..3cbb026 100644
> --- a/t/op/local.t
> +++ b/t/op/local.t
> @@ -5,7 +5,7 @@ BEGIN {
> @INC = qw(. ../lib);
> require './test.pl';
> }
> -plan tests => 306;
> +plan tests => 304;
>
> my $list_assignment_supported = 1;
>
> @@ -644,13 +644,6 @@ while (/(o.+?),/gc) {
> eval { local $1 = 1 };
> like($@, qr/Modification of a read-only value attempted/);
>
> -eval { for ($1) { local $_ = 1 } };
> -like($@, qr/Modification of a read-only value attempted/);
> -
> -# make sure $1 is still read-only
> -eval { for ($1) { local $_ = 1 } };
> -like($@, qr/Modification of a read-only value attempted/);
> -
> # The s/// adds 'g' magic to $_, but it should remain non-readonly
> eval { for("a") { for $x (1,2) { local $_="b"; s/(.*)/+$1/ } } };
> is($@, "");
>
>
>
--
perl -Mre=debug -e "/just|another|perl|hacker/"
Thread Previous
|
Thread Next