Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

XS hash question

by Anonymous Monk
on May 24, 2001 at 03:26 UTC ( [id://82770]=perlquestion: print w/replies, xml ) Need Help??

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

Monks;
I have just started dabbling with XS, and I've run into a snag. I want to change the way I'm calling the XS routine to use named parameters instead of positional parameters. So instead of this:
calculate('V_MIN',4.5);
I can do this:
calculate(NAME => 'V_MIN', THETA => 4.5);
I've looked at perlxs, but haven't found a way to do this. I want to be anle to get the values from the passed-in hash for keys "NAME" and "THETA". Here's my best quess thus far:
int calculate(hash) HV * hash; CODE: char * name; float theta; name = (char *)hv_fetch(hash,"NAME",4,0); theta = hv_fetch(hash,"THETA",5,0); printf("%s,%.1f\n",name,theta); . . . RETVAL = 1; OUTPUT: RETVAL
...but it won't even compile. I get errors:
ChemC.xs: In function `XS_ChemC_calculate': ChemC.xs:122: parse error before `char' ChemC.xs:125: `name' undeclared (first use in this function) ChemC.xs:125: (Each undeclared identifier is reported only once ChemC.xs:125: for each function it appears in.) ChemC.xs:126: `theta' undeclared (first use in this function) make: *** [ChemC.o] Error 1
Can anyone help?
Nelson

Replies are listed 'Best First'.
Re: XS hash question
by Anonymous Monk on May 24, 2001 at 08:01 UTC

    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

(tye)Re: XS hash question
by tye (Sage) on May 24, 2001 at 22:21 UTC

    Ugh, please, please don't implement this in XS. Just write a simple Perl wrapper that allows named arguments and have it call your XS routine with the arguments represented in a way that is easy (preferably trivial) for you to get at them using C code.

    By using Perl code to define the interface, you will almost certainly end up with an interface that is easier to use from Perl code, easier to understand, debug, maintain, and enhance, and you won't be doing things in XS that are likely to break with updates to Perl or not work when given "magic" things like tied hashes, etc.

    I went back and forth for a while on whether named parameters should be passed as a list of items or as a hash reference. After lots of cases I decided that the hash reference is more versatile and so I always use that now.

            - tye (but my friends call me "Tye")
Re: XS hash question
by robsv (Curate) on May 25, 2001 at 03:27 UTC
    If you want to use hashrefs, and choose to ignore tye's advice (in favor of speed?), you can get also the key values using hv_fetch:
    int calculate(hashref) SV * hashref; INIT: HV * myhash; SV **svp; int keylen; CODE: char * name; float theta; myhash = (HV *)SvRV(hashref); svp = hv_fetch(myhash,"NAME",4,0); if (svp) name = SvPV(*svp,keylen); svp = hv_fetch(myhash,"THETA",5,0); if (svp) theta = SvNV(*svp); printf("%s,%.1f\n",name,theta); . . . RETVAL = 1; OUTPUT: RETVAL
    If you're determined to use hashes, check out perlguts, and the book "Advanced Perl Programming" by Sriram Srinivasan.

    - robsv

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://82770]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-18 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found