xs function to get to tie hash

Discussion in 'Perl Misc' started by Tassilo v. Parseval, Dec 17, 2005.

  1. Also sprach :

    > 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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Dec 17, 2005
    #1
    1. Advertising

  2. Tassilo v. Parseval

    Guest

    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?
    , Dec 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pillbug
    Replies:
    2
    Views:
    475
  2. SpecialK

    Tie a file to a hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    1
    Views:
    461
    Ben Morrow
    Feb 19, 2004
  3. SpecialK

    Tie a large file to a Hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    446
    SpecialK
    Feb 19, 2004
  4. rp
    Replies:
    1
    Views:
    493
    red floyd
    Nov 10, 2011
  5. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    441
    botfood
    Apr 26, 2006
Loading...

Share This Page