Kang-min Liu <
[email protected]> writes:
Rainer Weikusat <[email protected]> writes:
# [1]
local *_ = \$_[0];
... to the scalar the reference came from: Afterwards (while the local is in >> scope) $_ 'means' 'the scalar passed as first argument': It now has a
name but its contents weren't copied.
I wonder how it compares to this alternative:
# [2]
local $_ = $_[0];
I guess [1] is aliasing and [2] is copying, although I'm not 100%
confident about this (nor the exact meaning of aliasing.)
----
use Devel::Peek;
$a = 'Hi';
sub aa
{
local $_ = $_[0];
Dump($_);
}
sub bb
{
local *_ = \$_[0];
Dump($_);
}
Dump($a);
aa($a);
bb($a);
-----
For a sufficiently recent perl (tested on 5.24) the output will be
SV = PV(0x556a0e4dfb20) at 0x556a0e4fc7c8
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK)
PV = 0x556a0e507250 "Hi"\0
CUR = 2
LEN = 10
COW_REFCNT = 1
SV = PV(0x556a0e4dfc00) at 0x556a0e4def48
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK)
PV = 0x556a0e507250 "Hi"\0
CUR = 2
LEN = 10
COW_REFCNT = 2
SV = PV(0x556a0e4dfb20) at 0x556a0e4fc7c8
REFCNT = 2
FLAGS = (POK,IsCOW,pPOK)
PV = 0x556a0e507250 "Hi"\0
CUR = 2
LEN = 10
COW_REFCNT = 1
The aa subroutine creates a new scalar sharing the body of the other via copy-on-write, the bb routine uses the passed scalar directly.
On older perls, aliasing in this way used to become faster than copying
the string for long strings (>> 100 characters). This is apparently
no longer the case when COW is supported.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)