develooper Front page | perl.perl5.porters | Postings from May 2004

Re: my $x->{foo} doesn't work

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
May 21, 2004 03:56
Subject:
Re: my $x->{foo} doesn't work
Message ID:
20040521110100.GB1904@iabyn.com
On Fri, May 21, 2004 at 10:47:54AM +0200, Rafael Garcia-Suarez wrote:
> tetryl@tokyoprogrammer.com wrote:
> > 
> > I love my variable and am looking for various ways of using it.
> > I will show you a bit of code.
> > 
> > $ perl -e 'my $x;$x->{foo}'     # OK
> > $ perl -e '(my $x)->{foo}'      # NG
> > 
> > I expected that autovivification would be applied to the
> > variable in the later case as well as in the first case, but
> > the results were different.
> 
> If you look at what contexts permit and forbid autovivification, you'll
> see that the situation in perl is currently complex and probably not
> very consistent. Anyway, in general lvalue contexts don't permit it; a
> lexical declaration can be seen as this; and you still can write, in a
> rather obfuscated way,

Thinking about this some more; it could be argued that since op.c has already
gone to the trouble of marking the padsv op with the OPpDEREF flag, that
its a bug in pp_padsv for then ignoring that flag if it happens also to
be marked with OPpLVAL_INTRO. That is to say,  ($x)->{foo} autovivifies
but (my $x)->{foo} doesn't. Clearly the OP's patch was wrong because it
threw out the baby with the bathwater, but maybe something like the
following (untested):


--- pp_hot.c	Mon May  3 23:57:33 2004
+++ pp_hot.c+	Fri May 21 11:58:22 2004
@@ -205,7 +205,7 @@
     if (PL_op->op_flags & OPf_MOD) {
 	if (PL_op->op_private & OPpLVAL_INTRO)
 	    SAVECLEARSV(PAD_SVl(PL_op->op_targ));
-        else if (PL_op->op_private & OPpDEREF) {
+        if (PL_op->op_private & OPpDEREF) {
 	    PUTBACK;
 	    vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
 	    SPAGAIN;
-- 
"You're so sadly neglected, and often ignored.
A poor second to Belgium, When going abroad."
    -- Monty Python - "Finland"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About