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

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)

3.4. List Assignment

In much the same way as scalar values may be assigned to variables, list values may also be assigned to variables:

($fred, $barney, $dino) = ("flintstone", "rubble", undef);

All three variables in the list on the left get new values, just as if we did three separate assignments. Since the list is built up before the assignment starts, this makes it easy to swap two variables' values in Perl:72


72) As opposed to in languages like C, which has no easy way to do this in general. C programmers usually resort to some kind of macro to do this, or use a variable to temporarily hold the value.

Hope this helps! :)

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

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re^7: Accessing Arguments inside Subroutines via @_
by Athanasius (Archbishop) on Mar 22, 2015 at 08:28 UTC

    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,