Front page | perl.perl5.porters |
Postings from May 2004
Re: ++(my $x = {}) [Was: Re: my $x->{foo} doesn't work]
Thread Previous
|
Thread Next
From:
Dan Kogai
Date:
May 21, 2004 13:43
Subject:
Re: ++(my $x = {}) [Was: Re: my $x->{foo} doesn't work]
Message ID:
78F466A6-AB67-11D8-A4F1-000A95DBB50A@dan.co.jp
On May 22, 2004, at 04:36, Ronald J Kimball wrote:
> On Sat, May 22, 2004 at 04:25:43AM +0900, Dan Kogai wrote:
>
>> 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.
>
> Maybe you should try that again with use strict.
Yes, use strict 'refs' does trap this.
> % perl -Mstrict=refs -le 'my $x=42; $x->[0]=3.14; print "$x, $x->[0]"'
> Can't use string ("42") as an ARRAY ref while "strict refs" in use at
> -e line 1.
That does explain what perl is actually doing. They just turn into
symbolic references.
> % perl -le 'my $x=42; $x->[0]=3.14; print ${"42"}[0]'
> 3.14
> % perl -le 'my $x=42; $x->[0]=3.14; print "42"->[0]'
> 3.14
The latter form is really funny. In a way you can use this as a hidden
global scalar.
That's also why the following worked.
On May 22, 2004, at 05:09, Dan Kogai wrote:
> But when you assign the same initial value it persists.... Maybe
> thanks to optimizer ?
>
>> % perl -le 'my $x=42; $x->[0]=3.14; $x->[0][0]=6.022e23;' \
>> -e '$x=0; print "$x, $x->[0], $x->[0][0]"'
>> 42, 3.14, 6.022e+23
>> % perl -le 'my $x=42; $x->[0]=3.14; $x->[0][0]=6.022e23;' \
>> -e '$x+=0; print "$x, $x->[0], $x->[0][0]"'
>> 42, 3.14, 6.022e+23
>> % perl -le 'my $x=42; $x->[0]=3.14; $x->[0][0]=6.022e23;' \
>> -e '$x=2*3*7; print "$x, $x->[0], $x->[0][0]"'
>> 42, 3.14, 6.022e+23
Okay. I understand. I think I can go to bed now. But IMHO '->' groks
symbolic references is a bad idea since we already have ${"sym"}
notation already....
Dan the Symbolically Referenced Man
Thread Previous
|
Thread Next