我想在我的 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;
}
Run Code Online (Sandbox Code Playgroud)
可能的解决方案之一可能是存储值本身,但可能有特殊的常量 forundef或 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 );
Run Code Online (Sandbox Code Playgroud)
&PL_sv_undef是undef标量。它是只读的。您可能需要一个新的 undef 标量,使用newSV(0)[1]创建。
返回的标量以newSV(0)引用计数 1 开始,当标量使用 存储在其中时,散列“占据”其中hv_stores,因此不要使用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
Run Code Online (Sandbox Code Playgroud)