如何在 Perl 哈希中存储空值
How to store null in Perl hash
我想在我的 C 代码 (XS) 中使用 Perl 散列作为一个集合,所以我只需要将键保留在散列中。是否可以存储 null 或其他常量值以避免创建不必要的值?
像这样:
int add_value(HV *hash, SV *value)
{
// just an example of key
char key[64];
sprintf(key, "%p", value);
if (hv_exists(hash, key, strlen(key)) return 0;
// here I need something instead of ?
return hv_stores(hash, key, ?) != NULL;
}
可能的解决方案之一是存储值本身,但也许 undef
或 null 有特殊常量。
&PL_sv_undef
是未定义的值,但不幸的是,您不能天真地在哈希和数组中使用它。引用 perlguts:
一般来说,如果你想在 AV 或 HV 中存储一个未定义的值,你不应该使用 &PL_sv_undef ,而是使用 newSV 函数创建一个新的未定义值,因为示例:
av_store( av, 42, newSV(0) );
hv_store( hv, "foo", 3, newSV(0), 0 );
&PL_sv_undef
是 undef 标量。它是只读的。您可能需要 a 新的 undef 标量,使用 newSV(0)
[1].
创建
newSV(0)
返回的标量以 1 的引用计数开始,其中使用 hv_stores
存储标量时的散列 "takes possession",所以不要 SvREFCNT_dec
或 sv_2mortal
返回的标量。 (如果您也将其存储在其他地方,请务必增加引用计数。)
# "The" undef (A specific read-only variable that will never get deallocated)
$ perl -MDevel::Peek -e'Dump(undef)'
SV = NULL(0x0) at 0x3596700
REFCNT = 2147483641
FLAGS = (READONLY,PROTECT)
# "An" undef (It's not the type of SVt_NULL that make it undef...)
$ perl -MDevel::Peek -e'Dump($x)'
SV = NULL(0x0) at 0x1bb7880
REFCNT = 1
FLAGS = ()
# Another undef (... It's the lack of "OK" flags that make it undef)
$ perl -MDevel::Peek -e'$x="abc"; $x=undef; Dump($x)'
SV = PV(0x3d5f360) at 0x3d86590
REFCNT = 1
FLAGS = ()
PV = 0
我想在我的 C 代码 (XS) 中使用 Perl 散列作为一个集合,所以我只需要将键保留在散列中。是否可以存储 null 或其他常量值以避免创建不必要的值?
像这样:
int add_value(HV *hash, SV *value)
{
// just an example of key
char key[64];
sprintf(key, "%p", value);
if (hv_exists(hash, key, strlen(key)) return 0;
// here I need something instead of ?
return hv_stores(hash, key, ?) != NULL;
}
可能的解决方案之一是存储值本身,但也许 undef
或 null 有特殊常量。
&PL_sv_undef
是未定义的值,但不幸的是,您不能天真地在哈希和数组中使用它。引用 perlguts:
一般来说,如果你想在 AV 或 HV 中存储一个未定义的值,你不应该使用 &PL_sv_undef ,而是使用 newSV 函数创建一个新的未定义值,因为示例:
av_store( av, 42, newSV(0) );
hv_store( hv, "foo", 3, newSV(0), 0 );
&PL_sv_undef
是 undef 标量。它是只读的。您可能需要 a 新的 undef 标量,使用 newSV(0)
[1].
newSV(0)
返回的标量以 1 的引用计数开始,其中使用 hv_stores
存储标量时的散列 "takes possession",所以不要 SvREFCNT_dec
或 sv_2mortal
返回的标量。 (如果您也将其存储在其他地方,请务必增加引用计数。)
# "The" undef (A specific read-only variable that will never get deallocated) $ perl -MDevel::Peek -e'Dump(undef)' SV = NULL(0x0) at 0x3596700 REFCNT = 2147483641 FLAGS = (READONLY,PROTECT) # "An" undef (It's not the type of SVt_NULL that make it undef...) $ perl -MDevel::Peek -e'Dump($x)' SV = NULL(0x0) at 0x1bb7880 REFCNT = 1 FLAGS = () # Another undef (... It's the lack of "OK" flags that make it undef) $ perl -MDevel::Peek -e'$x="abc"; $x=undef; Dump($x)' SV = PV(0x3d5f360) at 0x3d86590 REFCNT = 1 FLAGS = () PV = 0