liz has asked for the wisdom of the Perl Monks concerning the following question:
From what I've been able to learn from looking at a bunch of XS code, and [hs]v.[ch], the following code should do the trick:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = Data::Reuse PACKAGE = Data::Reuse void key_as_sv( hv, key, val ) HV *hv SV *key SV *val PROTOTYPE: \%$$ CODE: SvREFCNT_inc(key); SvREFCNT_inc(val); STRLEN length = SvCUR(key); hv_store( hv, SvPV( key, length ), length , val, 0 ); HE *he = hv_fetch_ent( hv, key, 0, 0 ); HeSVKEY_set( he, key );
which would then be called thus:
my %hash; my $foo = 'foo'; Data::Reuse::key_as_sv( %hash, $foo, $foo ); print STDERR "found ".(keys %hash)." keys:\n"; print STDERR " $_: $hash{$_}\n" foreach keys %hash;
However, this produces the following output:
or, in other words, the value of the key is gone.found 1 keys: foo:
I guess my question to any XS savvy Perl Monk is:
Thanks in advance for any insights.
Liz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using SV as key in hash
by Fletch (Bishop) on Jul 17, 2006 at 01:41 UTC | |
by liz (Monsignor) on Jul 17, 2006 at 06:16 UTC | |
|
Re: Using SV as key in hash
by grinder (Bishop) on Jul 17, 2006 at 08:11 UTC | |
by liz (Monsignor) on Jul 17, 2006 at 08:52 UTC | |
|
Re: Using SV as key in hash
by DrWhy (Chaplain) on Jul 17, 2006 at 19:17 UTC |