xs function to get to tie hash

  • Thread starter Tassilo v. Parseval
  • Start date
T

Tassilo v. Parseval

Also sprach (e-mail address removed):
When writing an xs extension I can allocate a blessed and tied hash
with
static SV *
_perlext_mkwrapper(void *ptr) {
HV *hash;
HV *stash;
SV *tie;
SV* ptrobj = newSViv(ptr);
hash = newHV();

tie = newRV_noinc((SV*)newHV()); << I want to get that hash

hv_store(SvRV(tie), _PERLEXT_CLASS_PTR, strlen(_PERLEXT_CLASS_PTR),
ptrobj, 0);
stash = gv_stashpv(_PERLEXT_CLASS, TRUE);
sv_bless(tie, stash);
hv_magic(hash, (GV*)tie, PERL_MAGIC_tied);
return newRV_noinc(hash);
}

But how can I get back to the tie hash (see mark above) if I get a
reference to svn _perlext_mkwrapper
created?

Is your question how to dereference 'tie' to get to the 'HV*' behind
that? The generic dereference macro in the perlapi is SvRV along with a
cast:

HV *hash = (HV*)SvRV(tie);

You will also usually want to check that it is in fact a reference to a
hash before dereferencing:

HV *hash;
if (SvTYPE(tie) == SVt_RV && SvTYPE(SvRV(tie)) == SVt_PVHV)
hash = (HV*)SvRV(tie);
else
croak("No hash reference");

Tassilo
 
E

eiselekd

When writing an xs extension I can allocate a blessed and tied hash
with
static SV *
_perlext_mkwrapper(void *ptr) {
HV *hash;
HV *stash;
SV *tie;
SV* ptrobj = newSViv(ptr);
hash = newHV();

tie = newRV_noinc((SV*)newHV()); << I want to get that hash

hv_store(SvRV(tie), _PERLEXT_CLASS_PTR, strlen(_PERLEXT_CLASS_PTR),
ptrobj, 0);
stash = gv_stashpv(_PERLEXT_CLASS, TRUE);
sv_bless(tie, stash);
hv_magic(hash, (GV*)tie, PERL_MAGIC_tied);
return newRV_noinc(hash);
}

But how can I get back to the tie hash (see mark above) if I get a
reference to svn _perlext_mkwrapper
created?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top