Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Accessing Arguments inside Subroutines via @_

by citi2015 (Initiate)
on Mar 20, 2015 at 03:07 UTC ( [id://1120689]=note: print w/replies, xml ) Need Help??


in reply to Re: Accessing Arguments inside Subroutines via @_
in thread Accessing Arguments inside Subroutines via @_

Thanks a lot, Athanasius.

No design decision behind it. A newer read that code from one book and find this error, I just realise I cannot explain. so I do my test and post the question on the community.

Thanks again for the help.

  • Comment on Re^2: Accessing Arguments inside Subroutines via @_

Replies are listed 'Best First'.
Re^3: Accessing Arguments inside Subroutines via @_
by Athanasius (Archbishop) on Mar 20, 2015 at 07:54 UTC

    Hello citi2015,

    I meant Perl’s design decision to remove the aliasing when @_ is assigned-to within a subroutine.

    You can, of course, do the swap by explicitly accessing the subroutine arguments as individual elements of @_:

    sub swap { my $temp = $_[0]; $_[0] = $_[1]; $_[1] = $temp; }

    — but note the necessity of “remembering” the initial value of $_[0] by storing it in a temporary variable. And I think that answers my question: if the aliasing were not removed, @_ = reverse @_ would produce wrong results, because some elements would be changed (assigned-to) before they were assigned-from.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Sorry, your theory is wrong.

      A list assignment doesn't need a $temp var and there is no race condition.

      DB<100> sub rev { ($_[0],$_[1]) = ($_[1],$_[0]) } DB<101> @a=("A","B") => ("A", "B") DB<102> rev @a DB<103> @a => ("B", "A")

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

        Hello LanX,

        Thanks for that, it’s good to know. Perl continues to surprise! I looked in the Camel Book, but the section on “List Assignment”1 doesn’t cover this behaviour. Do you know where it’s documented?

        I’m intrigued to know how Perl accomplishes this. I wrote the following, but it left me none the wiser:

        It looks as though the virtual machine:

        1. accesses the right-hand variables
        2. accesses the left-hand variables
        3. performs the assignments

        So it must create temporary copies of the right-hand variables “under the hood”?

        1Chapter 2, pages 82–83 in the 4th Edition.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Since perl can do multiple assignments in one swell foop, this would work too, avoiding the temporary variable:

      sub swappy { ($_[0], $_[1]) = ($_[1], $_[0]) }

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1120689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found