http://www.perlmonks.org?node_id=1004364


in reply to xs modifying passed value

Results after hacking up Bulk88's righteous work into my deeper needs!

Mucho Thanks for the kick in the git go.

int MyLib::strToInt(...) PREINIT: SV* passfailSV; bool passfail; CODE: if(items == 2) { SV* passfailSV = ST(1); RETVAL = THIS->strToInt(&passfail); SvSetMagicSV(passfailSV, passfail ? &PL_sv_yes : &PL_sv_no); } else if(items == 1) { RETVAL = THIS->strToInt(); } else croak("Usage: MyLib->strToInt(...)"); OUTPUT: RETVAL

Critiques ??
Comments ??
Suggestions ??
Improvements ??

-Enjoy
fh : )_~

Replies are listed 'Best First'.
Re^2: xs modifying passed value
by bulk88 (Priest) on Nov 18, 2012 at 02:03 UTC
    Considered having the return of MyLib::strToInt be the bool, and the param be the int? Usually subs/funcs return true false, and details of the operation are inplace updates/ pass by reference/ $_[0] = "hello";/*status = 1;/. Or the author of C++ method strToInt did it that way and thats the way you will remember it in Perl? You could also return 2 elements "($truefalse, $theint) = $instance->strToInt();", and have MyLib::strToInt method take only one param (THIS scalar). If the caller in perl doesn't want the int, they dont need to collect it in list context.

    edit: With XS/C/Perl internals, the biggest XSUB debugging tool is looking at the .c code. Try posting that. For C code, you should learn how to create post C processor code, then run it through a code formatter. A 7 character named macro taking 1 param, barely or terribly documented, can expand to a screenful of code after 2-5 layers of macros are expanded. To understand why it failed, looking at the post preprocessor output it the only way. Also compile with optimize=debug and symbols, as you said already, you have a C debugger, so thats correct. Also instead of returning 1 bool, or a list, consider returning a hashref, a hashref is probably overkill for this function.
      Some people for param lists do,
      my %localdog; if(@_ == 2 && ref($_[1]) eq 'HASH') { #named params my $hvref = $_[1]; $localdog{name} = $hvref->{name}; $localdog{color} = $hvref->{color}; } else{ $localdog{name} = $_[1]; $localdog{color} = $_[2]; }