Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Returning external library status codes from an XS routine

by pfaut (Priest)
on Nov 25, 2008 at 01:00 UTC ( #725738=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing an XS interface to a library and could use some advice on how to return error information.

The routines in the library return 0 if the call succeeded or a status value if there was an error. The library contains a routine to convert the status codes into strings that can be displayed for a user.

My interface routines mostly collect the information into a hash and return a reference to the caller. In case of error I'm returning an empty list. I would like to make the error information available to the caller because it could be very meaningful in certain cases (e.g., 'end of file' vs. other I/O error conditions).

I can't really return the status in $! since it isn't an errno value. I can return the error text in $@ but then the programmer would have to parse the error text rather than acting on an error code.

I'm contemplating replacing my simple handle object with a structure that can contain the handle and the last status returned by a library call using that handle. I could then add an XS routine to return that status to the calling program. The user could call the status fetch routine if one of the other calls returned undef. But this still doesn't help the open call that creates the handle (if the handle doesn't exist yet, I can't use it to hold status).

Any ideas?

90% of every Perl application is already written.

Replies are listed 'Best First'.
Re: Returning external library status codes from an XS routine
by ikegami (Pope) on Nov 25, 2008 at 01:20 UTC

    I can't really return the status in $! since it isn't an errno value.

    No, but nothing is stopping you from making your own global (package) variable or object attribute. For example, DBI has $DBI::err and $DBI::errstr.

    You could throw an exception object, where one method returns an error code and another returns an error string.

    You could return an error object that overrides booleanification (false on error), numification (error code) and stringification (error message), but that's probably too magical.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://725738]
Approved by ikegami
Front-paged by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2021-09-23 21:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found