in reply to perlsub question..

The behaviour I see, on "perl, v5.8.8 built for MSWin32-x86-multi-thread" is not consistent with the documentation the OP posted.
> perl -e "my @y;$y[3]=undef; show(@y); x(@y);show(@y);sub show{my $co +unt=0;print $count++, qq[ : $_;\n] for @_;print qq[----\n\n]}; sub x +{ @_[3,4,5,6,8]=(3,4,5,6,939);show(@_)}" --output-- 0 : ; 1 : ; 2 : ; 3 : ; ---- 0 : ; 1 : ; 2 : ; 3 : 3; 4 : 4; 5 : 5; 6 : 6; 7 : ; 8 : 939; ---- 0 : ; 1 : ; 2 : ; 3 : 3; ----
I would document the observed behaviour as :
Array elements are NOT created. Existing elements (which were EXPLICITLY created) will retain modifification in the sub.

     "An undefined problem has an infinite number of solutions." - Robert A. Humphrey         "If you're not part of the solution, you're part of the precipitate." - Henry J. Tillman

Replies are listed 'Best First'.
Re^2: perlsub question..
by Jenda (Abbot) on Jun 23, 2007 at 19:00 UTC

    You can't pass an array to a subroutine! You can pass the values of an array (which is what you did) or a reference to an array. Consider this:

    sub foo { print "@_\n"; push @_, 99; } @a = (1,2,4); foo(@a, 666)

    Now what's Perl supposed to do with this? Keep in mind that the foo() did not get two parameters, it got FOUR. The three elements of @a and the 666. The first three modifiable, the last readonly. No array was passed.

    The docs talk about things like foo( $a[99]) and foo( $h{new_key}), where in an old version the @a would be expanded and the 'new_key' key added into the %h upon calling the subroutine, while in the new versions this is only done if necessary.

    This is consistent with things like if (defined($a[99]){... versus $a[99] = 0;.

      Got it (++).

      So, would this statement be right :
      Elements of @_ are aliased to EXISTING (I was going to say DEFINED, but elements with the value 'undef' are updatable) elements of the passed parameter.

      This would explain why the indexes 5,6 and 8 never get created in the code I posted.

           "An undefined problem has an infinite number of solutions." - Robert A. Humphrey         "If you're not part of the solution, you're part of the precipitate." - Henry J. Tillman

        Elements of @_ are aliased to EXISTING/DEFINED elements of the passed parameter.
        Not necessarily.
        perl -MData::Dump::Streamer -le '@ary = (); x($ary[5],"foo"); sub x { +$_[0] = $_[1] }; Dump(\@ary)' $ARRAY1 = [ ( undef ) x 5, 'foo' ]; perl -MData::Dump::Streamer -le '@ary = (); x($ary[5],"foo"); sub x { +$_[0] }; Dump(\@ary)' $ARRAY1 = [];

        As you can see, the element at index 5 is not created merely by passing its alias to the sub. It doesn't exist and isn't defined, and may remain so after the sub has been called.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}