in reply to Re^3: perlsub question..
in thread perlsub question..

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}

Replies are listed 'Best First'.
Re^5: perlsub question..
by NetWallah (Canon) on Jun 24, 2007 at 14:38 UTC
    You make a valid point(++), so I will modify my proposed behaviour statement to:

    Elements of @_ are aliased to EXISTING or EXPLICITLY-passed element(s) of the passed parameter(s).
    The sub may modify ONLY THESE elements. Attempts to modify othe elements will not be retained on sub exit.

         "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

      You still seem to be thinking you can pass an array. "Existing elements of the passed parameters" only makes sense if you think foo(@bar) in some way passes @bar to foo. And it doesn't; @bar expands to a list of its elements, with anonymous undefined values in place of nonexistent elements. But I don't know that this behavior is set in stone, so I'd be reluctant to see it documented.
        This is precisely the mental model of parameter passing, that I'm trying to develop. Not the @arry passing part - - That's fine - you pass a list.

        Your statement :
              @bar expands to a list of its elements, with anonymous undefined values in place of nonexistent elements
        is negated by the code I posted. If "nonexistent elements" were indeed passed, assigning values to the corresponding aliases would vivify, in the original array. This is not the case.

        The only elements that can get persistent new values from inside the sub are "existing" elements, or explicitly passed elements, as Jenda indicated.

        I agree that it would be crufty to take advantage of this in production code. I also think this behaviour is "correct", and sufficiently documented for most code, and can be persuaded that since the behaviour is subject to change, it should not be encouraged/documented. I'm just trying to understand what the behaviour is, and trying to ensure that I have not mis-understood it.

             "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