Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Accessing Arguments inside Subroutines via @_

by Athanasius (Archbishop)
on Mar 20, 2015 at 07:54 UTC ( [id://1120702]=note: print w/replies, xml ) Need Help??


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

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,

Replies are listed 'Best First'.
Re^4: Accessing Arguments inside Subroutines via @_
by LanX (Saint) on Mar 20, 2015 at 21:11 UTC
    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,

        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!

Re^4: Accessing Arguments inside Subroutines via @_
by roboticus (Chancellor) on Mar 20, 2015 at 22:35 UTC

    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://1120702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found