根据Scalar::Util's documentation,refaddr
的工作原理如下:
my $addr = refaddr( $ref );
如果引用$ ref,则引用值的内部存储器地址将作为普通整数返回。否则返回undef。
但是,这并不能告诉我$addr
是永久性的。引用的refaddr
会随着时间的推移而改变吗?例如,在C中,运行realloc
可能会更改存储在动态内存中的内容的位置。这与Perl 5类似吗?
我问,因为我想制作一个inside-out object,我想知道refaddr($object)
是否能成为一把好钥匙。例如,在XS中编程时,它似乎最简单。
答案 0 :(得分:6)
首先,不要重新发明轮子;使用Class::InsideOut。
这是永久性的。必须是,否则以下会失败:
my $x;
my $r = \$x;
... Do something with $x ...
say $$r;
Scalars有一个" head"在固定的位置。如果SV需要升级(例如保持字符串),它就是第二个存储块,称为" body"那会改变。字符串缓冲区是第三个内存块。
$ perl -MDevel::Peek -MScalar::Util=refaddr -E'
my $x=4;
my $r=\$x;
say sprintf "refaddr=0x%x", refaddr($r);
Dump($$r);
say "";
say "Upgrade SV:";
$x='abc';
say sprintf "refaddr=0x%x", refaddr($r);
Dump($$r);
say "";
say "Increase PV size:";
$x="x"x20;
say sprintf "refaddr=0x%x", refaddr($r);
Dump($$r);
'
refaddr=0x2e1db58
SV = IV(0x2e1db48) at 0x2e1db58 <-- SVt_IV variables can't hold strings.
REFCNT = 2
FLAGS = (PADMY,IOK,pIOK)
IV = 4
Upgrade SV:
refaddr=0x2e1db58
SV = PVIV(0x2e18b40) at 0x2e1db58 <-- Scalar upgrade to SVt_PVIV.
REFCNT = 2 New body at new address,
FLAGS = (PADMY,POK,IsCOW,pPOK) but head still at same address.
IV = 4
PV = 0x2e86f20 "abc"\0 <-- The scalar now has a string buffer.
CUR = 3
LEN = 10
COW_REFCNT = 1
Increase PV size:
refaddr=0x2e1db58
SV = PVIV(0x2e18b40) at 0x2e1db58
REFCNT = 2
FLAGS = (PADMY,POK,pPOK)
IV = 4
PV = 0x2e5d7b0 "xxxxxxxxxxxxxxxxxxxx"\0 <-- Changing the address of the string buffer
REFCNT = 2 doesn't change anything else.
CUR = 20
LEN = 22