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

Hi - How do I set $! in an xs method? My reading of perlxstut, perlxs, and perlguts leads me to believe it's tied in with error and _sys_errlist; but I want to set $! to a meaningful string for my module.

Thanks - this is my first post ;-)

Replies are listed 'Best First'.
Re: Setting $! in xs
by Prof Vince (Friar) on Aug 06, 2011 at 20:48 UTC
    Just assign to errno in your C code. $! is magic, so it will be automatically updated the next time it is evaluated in Perl.

    But you should do that only if the error code you want to pass this way is a meaningful errno code (for example, if it is given after a system call). For all the other cases, an exception is a much saner solution.
Re: Setting $! in xs
by Anonymous Monk on Aug 06, 2011 at 14:35 UTC
    Don't, that is not the purpose of $!, simply croak instead
      Well, if you still wanted to, you could
      sv_setpvf( get_sv("!", TRUE), "blah %d %s", errno, strerror(errno) );
Re: Setting $! in xs
by strredwolf (Chaplain) on Aug 06, 2011 at 17:33 UTC
    Wouldn't $? be better off, or setting an internal error that your XS would provide access to?
    Information doesn't want to be free. It wants to be feline.
Re: Setting $! in xs
by Vitantonio (Initiate) on Aug 07, 2011 at 06:33 UTC

    Thanks all...

    I guess I was trying to get too clever, I'll build my method to return a meaningfull error string.