in reply to Re^4: Accessing Arguments inside Subroutines via @_
in thread Accessing Arguments inside Subroutines via @_
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:
#! perl use strict; use warnings; use feature qw(say state); #say 'f = ', f(), ', g = ', g(); ( f(), g() ) = ( g(), f() ); #say 'f = ', f(), ', g = ', g(); sub f : lvalue { state $f = 'first'; say 'f(', $f, ')'; return $f; } sub g : lvalue { state $g = 'second'; say 'g(', $g, ')'; return $g; }
Output:
12:58 >perl 1193_SoPW.pl g(second) f(first) f(first) g(second) 12:58 >
It looks as though the virtual machine:
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, |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Accessing Arguments inside Subroutines via @_
by LanX (Saint) on Mar 21, 2015 at 12:09 UTC | |
by Athanasius (Archbishop) on Mar 22, 2015 at 08:28 UTC |