Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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.
dragonchild

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://725738]
Approved by ikegami
Front-paged by Old_Gray_Bear
help
Chatterbox?
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.
[thezip]: G'day all!
[perldigious]: Well, that and dodging the people who forget how ice works right away. :-)
[stevieb]: hey, thezip
[perldigious]: I don't miss having to drive up and down in elevation on roads that are iced over, I white knuckled gripping the steering wheel every time I had to where I used to live.
[perldigious]: So I don't envy you mountain guys if you have to drive in the winter, even with tire chains.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2016-12-06 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.