On Tue, Mar 11, 2003 at 08:58:35AM +0000, Steve Hay wrote:
> Just a quick question:
> 
> Is there a "preferred way" to return a Boolean value from an XSUB?
> 
> I could have an SV* function returning &PL_sv_yes/&PL_sv_no (or 
> &PL_sv_undef?), or a bool function returning TRUE/FALSE, or even just an 
> int function returning 1/0.
> 
> Is there any advantage in any of these?

There's a disadvantage in the last one, as an int return value will cause
a new SV to be allocated. TRUE/FALSE map to &PL_sv_yes/&PL_sv_no (IIRC), and
they (and &PL_sv_undef) don't need to be allocated (or subsequently freed).
Hence bool, or explicit &PL_sv_yes/&PL_sv_no will be faster. Or you can
use the equivalent XSRETURN_NO/XSRETURN_YES.

If you're subroutine is meant to be called in a list context, you may
want to use XSRETURN_EMPTY, as XSRETURN_UNDEF is equivalent to

sub foo {
  ...
  return undef;
}

and is returning a true value if used in a list context (a list with one
entry)

Nicholas Clark

Reply via email to