A bit of a different explanation...

Let's pretend we are in Perl4 so there are no \ and -> operators to deal with references so we can use them to talk about how Perl itself deals with aliases.

Then calling test($x,$y,$z) sets @_ to (\$x,\$y,\$z). So if you do $_[0]= "new val" without having modified @_ then you end up doing ${\$x}= "new val" because Perl will see that $_[0] currently contains a reference to (alias of) $x and so modifying $_[0] also modifies $x.

Well if you do my $x1= shift then you are left with @_ containing (\$y,\$z). Do that two more times and @_ is empty. So $_[0]= "new val" ends up putting a value into @_ where no value was before. So there is no reference (alias) for Perl to implicitly dereference so, of course, $x is not changed in that case. So you end up with @_ having ("new val") or ("x2","y2","z2") and @_ just gets thrown away in the end.

Also, when @_ is empty, doing $_[0]= "x" finds $_[0] not being an alias to anything and so creates a new scalar and sets $_[0] to be an alias to it (the only alias at that point).

I hope that helps some.

Update: And calling test(@v) sets @_ to (\$v[0],\$v[1],\$v[2]) (if @v has three elements) (which could be written as \(@v) in Perl5). But doing @_= @v copies the values rather than making aliases.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Calling subroutine in a package from other perl sub. by tye
in thread Calling subroutine in a package from other perl sub. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.