in reply to Re^2: Putting weird things into $#array
in thread Putting weird things into $#array

That doesn't explain why $#a is behaving so oddly when I retrieve its value.

When you set $#a, Perl needs an integer. If you give it something that's more than just an integer, it takes out the integer part and stores that in the appropriate place in the data structure it uses to represent the array. It throws out the rest of the information, as it doesn't need it.

When you read $#a, Perl looks in the underlying data structure and fetches the integer stored there. It returns it. It can't return any more information. It doesn't store it.

This is C-level stuff. If you're curious, see av.h, specifically the xpvav struct declaration and the xav_fill member. It holds an integer (see uconfig.h, I think), not an SV.

Update: I did misread the question. The right code to read is that of the av2arylen opcode in pp.c, which I really don't follow. Consider this program though, which shows that $#a isn't a plain integer, it's an SV with magic:

use Devel::Peek; my @a; Dump( $#a ); my $b = {foo=>1}; exit if $b + 0 > 0x10125000; $#a=$b; Dump( $#a );

Replies are listed 'Best First'.
Re^4: Putting weird things into $#array
by samtregar (Abbot) on Mar 07, 2005 at 22:18 UTC
    Did you read his whole post? It looks like he's demonstrating that what you're saying isn't correct. If it was, how does this work:

    % perl -le '$b={foo=>1}; $#a=$b; print keys %{$#a}' foo

    If you're right and the $# slot is just an int then how did keys get a hash out of it?

    -sam

Re^4: Putting weird things into $#array
by blokhead (Monsignor) on Mar 07, 2005 at 22:22 UTC
    fetches the integer stored there. It returns it. It can't return any more information. It doesn't store it.
    Then how is it possible that in my examples, $#a is being (successfully) used a hard reference? Or am I misinterpreting the results?

    blokhead