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

Grettings all, I've decided to delve a bit in to programming with XSUBs. I wrote the C code, and the associated xs code. Everything complies fine and for the most part, runs fine, too. However, I'd like to be able to return undef to perl in case of failure. The C function that I have is prototyped as such:
char * rdw_read(FILE * input_file)
Also, while I'm at it, I'd like the corresponding perl code to be able to emulate the core readline function in so far as to be able to distinguish between scalar and array context.

thanks in advance,

thor

p.s. In case anyone was wondering, I'm trying to write code to deal with files that have rdw's in them.

Replies are listed 'Best First'.
Re: Returning undef from XS code
by Zaxo (Archbishop) on Aug 21, 2003 at 04:22 UTC

    See PL_sv_undef in perlapi. The wantarray-like construct is also documented there under GIMME_V.

    After Compline,
    Zaxo

Re: Returning undef from XS code
by edan (Curate) on Aug 21, 2003 at 07:13 UTC

    I have used XSRETURN_UNDEF in my XS code, which is also documented in perlapi. It just 'Returns "&PL_sv_undef" from an XSUB immediately.'. I don't know if one way is better than the other, I just thought I'd throw it out there...

    --
    3dan
Re: Returning undef from XS code
by Anonymous Monk on Aug 21, 2003 at 04:43 UTC
Re: Returning undef from XS code
by diotalevi (Canon) on Aug 21, 2003 at 13:56 UTC

    And of course, you'll be immensely happier (so the theory says) if you use Inline::C. The thing is - if your XS accepts input from perl then you have to deal with perl's entire magic system, ties, unicode, readonly values, etc. Yuck.

Re: Returning undef from XS code
by thor (Priest) on Aug 21, 2003 at 15:40 UTC
    The answers so far look promising. I do have an additional question. What value should I return from the C sub in order to indicate failure? It's probably a no-brainer, but I'm not a C programmer (though I play one on TV from time to time).

    thor