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

++(my $x = {}) [Was: Re: my $x->{foo} doesn't work]

Thread Previous | Thread Next
Dan Kogai
May 21, 2004 12:25
++(my $x = {}) [Was: Re: my $x->{foo} doesn't work]
Message ID:
On May 21, 2004, at 17:47, Rafael Garcia-Suarez wrote:
> 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,
>     (my $x = {}) -> {foo}
> Your patch looks correct at a first glance, but I'm not very inclined 
> to
> apply it, unless the crowd comes up with good reasons why to.

Speaking of autovivification, how about such cases in reverse, That is, 
when non-reference value is assigned with --, ++, +=, etc ?  i.e.

   perl -le '$x = {}; print ++$x'

You may expect '1' but this prints a random number, or numified version 
of "HASH(0xdeadbeef)" plus 1.  While this makes sense to those who 
knows camel anatomy, this is somewhat asymmetrical and confusing....

[A few minutes later...]

Hey, watch this!

> % perl -le 'my $x=42; $x->[0]=3.14; print $x; print $x->[0]; print 
> ref($x)'
> 42
> 3.14
> % perl -le 'my $x; $x->[0] = 3.14; print $x; print $x->[0]; print 
> ref($x)'
> ARRAY(0x801180)
> 3.14

See the difference?  The first one has 42 assigned a priori.  I have 
duplicated this even on good old 5.00503, not to mention 5.8.4.

Dan the Camel (?:Ab)user

P.S.  I found more of Hawakawa-kun's my abuses at

I only hope you don't do that in your PRODUCTION CODES....

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About