rsteinke has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to provide a tied hash interface to a C object in my XS code. The OUTPUT section of my typemap looks like

T_TIED_HASH { SV* tie = NEWSV(0, 0); HV* hash = newHV(); /* Get a blessed reference to the pointer, */ sv_setref_pv(tie, CLASS, (void*)$var); /* and tie it to a hash. */ hv_magic(hash, (GV*)tie, PERL_MAGIC_tied); $arg = newRV_noinc((SV*)hash); }
which I was expecting to return a tied HV reference. However, in my test code
my $obj = new TiedXS(1); ok($obj); ok(tied $obj);
the first test succeeds, but the second fails. Ideas?

Ron Steinke rsteinke@w-link.net

Replies are listed 'Best First'.
Re: Using hv_magic() in typemap to create a tied object
by ysth (Canon) on Aug 04, 2004 at 15:37 UTC
    What does
    use Devel::Peek; Dump $obj;
    show?
      Ah, silly me for not seeing it sooner. It's the hash that's tied, not your reference to it. Try tied(%$obj) instead.
      I get
      RV(0x8199c48) at 0x8163304 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x814cd74 SV = PVHV(0x8167888) at 0x814cd74 REFCNT = 2 FLAGS = (RMG,SHAREKEYS) IV = 0 NV = 0 MAGIC = 0x826f6b0 MG_VIRTUAL = &PL_vtbl_pack MG_TYPE = PERL_MAGIC_tied(P) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x814cc6c SV = RV(0x8199c34) at 0x814cc6c REFCNT = 2 FLAGS = (ROK) RV = 0x814cd98 SV = PVMG(0x81ae900) at 0x814cd98 REFCNT = 1 FLAGS = (OBJECT,IOK,pIOK) IV = 135647352 NV = 0 PV = 0 STASH = 0x8267a94 "TiedXS" ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0
      Ron Steinke rsteinke@w-link.net

        Ah, I see now. My mistake. The test should have been

        ok(tied %$obj)
        I was testing the hash reference for being tied, instead of the actual hash itself. It works now. Thanks!

        Ron Steinke rsteinke@w-link.net