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

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!

Replies are listed 'Best First'.
Re^5: Accessing Arguments inside Subroutines via @_
by Athanasius (Archbishop) on Mar 21, 2015 at 03:24 UTC

    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,

      Hello Athanasius!

      No idea about the camel book, I have to admit I never read it.

      my understanding is that the values from RHS are temporarily pushed on a stack like structure and from there assigned to the LHS.

      B::Concise shows

      ~$ perl -MO=Concise -e '($a,$b)=($b,$a)' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 9 <2> aassign[t5] vKS/COMMON ->a - <1> ex-list lKP ->6 3 <0> pushmark s ->4 - <1> ex-rv2sv sK/1 ->5 4 <#> gvsv[*b] s ->5 - <1> ex-rv2sv sK/1 ->- 5 <#> gvsv[*a] s ->6 - <1> ex-list lKPRM* ->9 6 <0> pushmark sRM* ->7 - <1> ex-rv2sv sKRM*/1 ->8 7 <#> gvsv[*a] s ->8 - <1> ex-rv2sv sKRM*/1 ->- 8 <#> gvsv[*b] s ->9 -e syntax OK

      so you might wanna look into ex-list (where both sides are stored) and aassign ("array-assign" which is a misnomer).

      Unfortunately I couldn't find a good compilation explaining all these ops.

      From the Llama (Learning Perl, 3rd Edition)

      Hope this helps! :)

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

      PS: Je suis Charlie!

        Hello LanX,

        No idea about the camel book, I have to admit I never read it.

        And I’ve never read the Llama Book! The quote from it is useful, thanks, especially “the list is built up before the assignment starts,” which (I think) confirms my conjecture that a list assignment creates temporary copies of the RHS elements before assigning them to the LHS.

        I’m afraid I can’t (yet) follow the output from B::Concise (and comparing it with the output from B::Terse doesn’t help me!), but one of these days I hope to read up on the Perl VM. As a first step I’m going to look at perlguts and perlinterp.

        Thanks for the help,

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