blokhead has asked for the wisdom of the Perl Monks concerning the following question:
Of course, setting $#a=$b has the (side-)effect of expanding @a to a huge size, so these examples take a few seconds (or may run out of memory even):
Can this actually be doing what I think it is?? Indeed, it looks as though $#a is actually the same reference as $b$:% perl -le '$b=[1..4]; $#a=$b; print $#{$#a}' 3
In fact, this also works when $b is lexical, so it's not a symbolic reference.% perl -le '$b=[1..4]; $#a=$b; print @{$#a}; push @$b, 5; print @{$#a} +' 1234 12345
You can put other types of references into $#a, and dereference them in any standard way:
However, you cannot preserve blessed-ness of a scalar:% perl -le '$b=[1..4]; $#a=$b; print $#a->[2]' 3 % perl -le '$b=\"foo"; $#a=$b; print ${$#a}' foo % perl -le '$b={foo=>1}; $#a=$b; print keys %{$#a}' foo
This is our first clue that this is not a fully-fledged reference.. we also see that ref is oblivioius to it:% perl -le 'sub foo{"foo"} $b=bless[]; $#a=$b; print $#a->foo' Can't call method "foo" without a package or object reference at -e li +ne 1.
Also, I can't copy $#a into a variable and then use that variable as a reference:% perl -le '$b=[1..4]; $#a=$b; print ref $#a' [nothing]
In fact, after I copy $#a into a variable, I can't even use $#a itself as a reference anymore! Hello Heisenberg!% perl -le '$b=[1..4]; $#a=$b; $x=$#a; print @$x' [nothing]
Very odd.% perl -le '$b=[1..4]; $#a=$b; $x=$#a; print @{$#a}' [nothing]
So, Does anyone know what's going on? I would have expected this to behave either kinda like hash keys (which are always string values, and are not retrieved as full scalars with the requisite magic) or just a normal scalar. What seems to be happening to $#a is a little bit of both.
Remember, this is purely for curiosity's sake, as using $#a as a reference is quite mad.
PS: It is a damn shame that the following code runs out of memory, it would make quite a bizarre JAPH:
(the out of memory error I get is another mystery -- if I replace the @ with an $#, I don't crash)#/usr/bin/perl -l my@a=qw[just another perl hacker];$#a=\@a; print "@{$#{$#{$#{$#{$#{$#{$#{$#a}}}}}}}}"
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Putting weird things into $#array
by chromatic (Archbishop) on Mar 07, 2005 at 21:50 UTC | |
by ikegami (Patriarch) on Mar 07, 2005 at 22:04 UTC | |
by blokhead (Monsignor) on Mar 07, 2005 at 21:58 UTC | |
by chromatic (Archbishop) on Mar 07, 2005 at 22:13 UTC | |
by samtregar (Abbot) on Mar 07, 2005 at 22:18 UTC | |
by blokhead (Monsignor) on Mar 07, 2005 at 22:22 UTC | |
by jonadab (Parson) on Mar 07, 2005 at 21:54 UTC | |
by poqui (Deacon) on Mar 08, 2005 at 22:23 UTC | |
|
Re: Putting weird things into $#array
by betterworld (Curate) on Mar 07, 2005 at 23:21 UTC | |
|
Re: Putting weird things into $#array
by Roy Johnson (Monsignor) on Mar 07, 2005 at 22:37 UTC | |
by bmann (Priest) on Mar 08, 2005 at 00:13 UTC | |
by Limbic~Region (Chancellor) on Mar 07, 2005 at 23:31 UTC | |
by Roy Johnson (Monsignor) on Mar 08, 2005 at 00:06 UTC | |
by Limbic~Region (Chancellor) on Mar 08, 2005 at 12:46 UTC |