Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Exceptions and Return Codes

by Abigail (Deacon)
on Jun 29, 2001 at 18:37 UTC ( #92642=note: print w/replies, xml ) Need Help??


in reply to Exceptions and Return Codes

Does anyone have any thoughts on how I should approach passing error conditions and return values around in my own code ?

That question doesn't belong in the spirit of Perl. If you want others to decide how you should approach things, you should be using Python, or Java. If you program Perl, you should use what you think is right for you.

I can however, tell you what I usually use. I tend to die for unexpected failures - for instance, failure to open a file, or wrong types of arguments. (But note that Dennis Ritchie once said that a failure to open a file was hardly exceptional). For failures that could be expected (for instance, a search in a datastructure that doesn't find anything), I typically return undef or an empty list, but sometimes 0, or if I want to give some information why a failure occured, I return some predefined constant - negative numbers, or strings, depending what is more appropriate. It should, after all, be possible to determine which return values signal failures, and which ones are normal return values. Sometimes I return a 2-element list, one element indicates success/failure, the other the return values or failure reason.

I tend not to use globals for explanations, except $@ and to a lesser extend $!. I never, never use warn to do internal message passing. warn IMO, communicates to the user, not the rest of the program. I may use warn in combination with the other failure flagging techniques.

Carp is just a wrapper around die and warn, and I do not consider that a different technique.

-- Abigail

Replies are listed 'Best First'.
Re (tilly) 2: Exceptions and Return Codes
by tilly (Archbishop) on Jun 29, 2001 at 20:33 UTC
    If that question does not belong in the spirit of Perl, then neither does Very very small style question from Dominus.

    Likewise the core documentation should cut out perlstyle, it is obviously encouraging the wrong attitudes towards Perl.

    Now if I want someone else to tell me how to program, then I will use Python or Java. But if I stop looking for suggestions on how I might be able to program better, then I will look for another career.

    Aside from that, I tend to do something similar to the rest of your answer except with the caveat that I think of Carp as being different from die and warn in that it makes it easy to get useful context. Also in CGI programming warn becomes a way to communicate with the server logs rather than the user. This distinction is often very useful.

Re: Re: Exceptions and Return Codes
by sierrathedog04 (Hermit) on Jun 29, 2001 at 19:41 UTC
    Abigail you are more knowledgeable and active in the Perl community than I am.

    But on this you are wrong. If I do not know what is the best way to handle failed subroutine calls, then it is reasonable for me to find out what others have determined is best for them.

    Because what is best for others who have approached the same problem is probably, 97 percent of the time, best for me. So I say that one should, when learning Perl, let the experience of others direct you when it is not clear what direction you should take on your own.

    What should you as a Perl programmer do when you are "on your own, with no direction home, a complete unknown, like a rolling stone?" Plagiarize the code of others, I say.

      You didn't ask what's best for them. You asked what you should use, without giving any information what kind of failures you wanted to signal.

      Plagiarize the code of others, I say

      That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad.

      -- Abigail

Re: Re: Exceptions and Return Codes
by kschwab (Vicar) on Jun 29, 2001 at 19:46 UTC
    That question doesn't belong in the spirit of Perl. If you want others to decide how you should approach things, you should be using Python, or Java. If you program Perl, you should use what you think is right for you.

    Aside from the above, lots of useful info, thanks.

    I do have to reply to the above though. What's that about? Looks like I touched a nerve. When I say "Does anyone have any thoughts...", I'm looking for suggestions and ideas from others. I'll use those suggestions along with my own thoughts to come to a conclusion. Since other folks might end up using code I've written, I was hoping to get a perspective on what they might expect.

    Yeesh.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://92642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2019-12-09 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?