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

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

Thread Previous | Thread Next
From:
Dan Kogai
Date:
May 21, 2004 12:25
Subject:
++(my $x = {}) [Was: Re: my $x->{foo} doesn't work]
Message ID:
A6E003C0-AB5C-11D8-A4F1-000A95DBB50A@dan.co.jp
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
> ARRAY

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

http://shibuya.pm.org/slides/bkcon2004/hayakawa.pdf

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


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