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

example is better than a long explanation

here goes...

$x = {};
callto_xsub($x)

in the xsub an HV * is created that will be the root for a hash of hashes. i.e.

$x = { key0 => { a => var1, b => var2, }, and so on.
The second level hashs are all created within the xsub with the following code snippet where 'dusr' is the HV for the 'key' for the secondary hash
hv_store(dusr,"C",1,newSVuv(1),0); hv_store(dusr,"B",1,newSVuv(len),0); hv_store(dusr,"E",1,newSVuv(now),0); hv_store(dusr,"T",1,newSVuv(0),0);
subsequently, the key "T" gets updated by a call_sv to a perl routine as follows:
if ((fetch_uv(dusr,"T")) < now) { dSP; PUSHMARK(SP); # trgt.s is the key for HV * dusr XPUSHs(sv_2mortal(newSVpv(trgt.s,4))); PUTBACK; call_sv(myperlsub,G_VOID); } in the perl routine, this instruction is executed $x->{trgt}->{T} = 12345; upon return from the perl call_sv, examination of the contents of the +sub hash vpp = hv_fetch(hp,trgt.s,1,0); value = SvUVX(*vpp); reveals that the contents appear to contain the initalization value ra +ther than 12345. HOWEVER, if the hash is iterated for(itmp = 0; itmp < iend;itmp++) { he = hv_iternext(dusr); ctmp = hv_iterkey(he,&rtmp); ttmp = SvUV(hv_iterval(dusr,he)); fprintf(stderr,"key %s => %d\n",ctmp,ttmp); } The correct value magically appears
So... my question is, how does one get the hash as it appears in the xsub to "update" or whatever is required to see the value that was stored when the call_sv was made to perl space?

please email: Michael@bizsystems.com ############ update Using Devel::Peek, I see what the problem is, but do not know how to fix it.

Upon initializing the sub hash, the values are created as UV's i.e.:

SV = IV(0x8380458) at 0x83b70d8 REFCNT = 1 FLAGS = (IOK,pIOK,IsUV) UV = 0 When the perlsub updates the data it is conveted to an NV SV = PVNV(0x8284298) at 0x83b70d8 REFCNT = 1 FLAGS = (NOK,pNOK) IV = 0 NV = 12345 PV = 0
The question no is how do I coerce perl into storing the value as a UV as I originally intended. The hash of hashes will be large and "doubles" is not what I had in mind ;-)

Michael@bizsystems.com

Edit by GrandFather - replaced pre tags with code tags, to prevent distortion of site layout and allow code extraction.

Replies are listed 'Best First'.
Re: updating hash from perl -> xsub
by davidrw (Prior) on May 21, 2006 at 04:59 UTC
    please email: Michael@<snip>.com
    Don't Give Us Your Email Address

    Also, if people were to answer via email, then the replies wouldn't be archived here for the benefit of everyone else that comes to this thread later on, circumventing the very nature of this site..
Re: updating hash from perl -> xsub
by borisz (Canon) on May 21, 2006 at 09:57 UTC
    Here is my frist try:
    It should work for all simple cases like '1234', 121, 12111.0
    use Inline 'C'; use Devel::Peek; my $x; my $v = 2147483648; Dump($v); $x->{T}{z} = force_iv($v); Dump($x->{T}{z}); __END__ __C__ SV* force_iv(SV*v){ return SvNOK(v) ? newSVuv(SvNV(v)) : newSVuv(SvIV(v)); }
    Boris
Re: updating hash from perl -> xsub
by GrandFather (Saint) on May 21, 2006 at 03:56 UTC

    It may be that there is a monk that recognises your problem domain and therefore understands what your pseudo code and cryptic abreviations mean and can help you. However, if you want a larger number of eyes to look at your problem without glazing I'd seriously consider writing a short test script that demonstrates your problem without obfusicating abreviations and unrunable code. It may help to read I know what I mean. Why don't you?.


    DWIM is Perl's answer to Gödel

      To be fair on the OP these aren't "pseudo code and cryptic abreviations" but XS code and the output from Devel::Peek which would be instantly familiar to anyone who has written XS or been involved with development of the Perl core.

      Of course there are a smaller number of potential readers who might be able to help with the problem but I am not sure that it could be expressed any differently.

      /J\

        Fair enough.

        My first reaction when reading through most of the sample code was: "This isn't Perl, it's C" (there's a reason for that). A heads up that this was in the context of XS would save a little wasted time, both for those of us trying to make sense of what, at first glance, looks like a C programer's first attempts at Perl, and those reading through the replies.

        True, it's not pseudo code, they are however cryptic abbreviations, and it is indeed true that indicating that the problem deals with XS code would save a few glazed eyes. :)


        DWIM is Perl's answer to Gödel