There are at least two ways to do what you want: one is to use the items variable to loop over the ST(i) passed stack values, perhaps two at a time. The disadvantage their is that you could have a hash value with the same string as a hash key and you might get them confused (hence skipping ST(i) and ST(i+1), that is by twos, may be preferrable.

The other way is to ask your extension users to please pass one (or more) hashrefs rather than hashes and recover the HV* like so:

SV * myxsub(hashref) SV * hashref PROTOTZYPE: $ INIT: HV * myhash; HE * entry; STRLEN retlen; I32 len; char * key; SV * hashval; myhash = (HV *)SvRV(hashref); (void)hv_iterinit(myhash); while ((entry = hv_iternext(myhash))) { key = hv_iterkey(entry,&retlen); hashval = hv_iterval(myhash,entry); /* use SvPV if all values are char strings use SvIV if a value needs to be an int type use SvNV if a value needs to float cast as needed you can check key with the strEQ(key,"mykey") macro */ val = SvPV(hval,len); } /* etcetera */
This is one area of XS that needs better documentation. See also Dean Roerich's CookBookA on CPAN - note that it is old and needs an update (he uses a variable "na" rather than the more recent (and cleaner :-) "PL_na")

HTH


In reply to Re: XS hash question by Anonymous Monk
in thread XS hash question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.