in reply to Re^3: Accessing Arguments inside Subroutines via @_
in thread Accessing Arguments inside Subroutines via @_
I took a look into the Panther book and experimented with Devel::Peek and the implementation of aliases seems simpler than a magic data structure of type alias.
Arrays (AVs) are composed from C-structures with pointers to C-structure(s) realizing scalars (SVs).
So $_[0]=666 updates the underlying scalar ( what I called "assigning" ) while @_=(666) creates the pointer to a new scalar ( "setting" ).
In the following dump the scalar value for $x is held within SV = IV(0x88f2498) at 0x88f249c
The array's first slot will point to that same structure before and after updating with $_[0]=666 (#markers added)
DB<126> use Devel::Peek DB<127> sub tst_update { Dump(\@_); $_[0]=666; Dump(\@_) } DB<128> my $x=42; Dump($x); tst_update $x SV = IV(0x88f2498) at 0x88f249c # <--- SV for $x REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 42 SV = IV(0x8d02430) at 0x8d02434 REFCNT = 1 FLAGS = (ROK) RV = 0x8a5cbbc SV = PVAV(0x89be074) at 0x8a5cbbc REFCNT = 3 FLAGS = () ARRAY = 0x86d4500 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x88f2498) at 0x88f249c # <--- SV for $x REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 42 SV = IV(0x8d02430) at 0x8d02434 REFCNT = 1 FLAGS = (ROK) RV = 0x8a5cbbc SV = PVAV(0x89be074) at 0x8a5cbbc REFCNT = 3 FLAGS = () ARRAY = 0x86d4500 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x88f2498) at 0x88f249c # <--- SV for $x REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 666
But creating new entries with @_=(666) will replace the pointer in this AV-slot to a new SV.
DB<129> sub tst_create { Dump(\@_); @_=(666); Dump(\@_) } DB<130> my $x=42; Dump($x); tst_create $x SV = IV(0x88f2740) at 0x88f2744 # <--- SV for $x REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 42 SV = IV(0x8d019a4) at 0x8d019a8 REFCNT = 1 FLAGS = (ROK) RV = 0x8a5cbbc SV = PVAV(0x89be074) at 0x8a5cbbc REFCNT = 3 FLAGS = () ARRAY = 0x86d4500 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x88f2740) at 0x88f2744 # <--- SV for $x REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 42 SV = IV(0x8d019a4) at 0x8d019a8 REFCNT = 1 FLAGS = (ROK) RV = 0x8a5cbbc SV = PVAV(0x89be074) at 0x8a5cbbc REFCNT = 3 FLAGS = () ARRAY = 0x86d4500 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x88f2718) at 0x88f271c # <--- SV for liter +al 666 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 666
I hope the "how" is clearer now! :)
|
---|