Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: What Are The Rules For Subs And Modules When Fed Wrong Input

by ichimunki (Priest)
on Jul 22, 2002 at 18:14 UTC ( #184164=note: print w/replies, xml ) Need Help??

in reply to What Are The Rules For Subs And Modules When Fed Wrong Input

For your private code you should use die. To quote the Pragmatic Programmers, "Crash Early: A dead program normally does a lot less damage than a crippled one." If you are writing OO code, consider using and croak() or confess() so that the programmer gets a better idea of what went wrong (stack trace, etc).

For your public code you should also die. Although in some cases you may want to wrap that in some sort of eval to provide services to your users similar to the way CGI can be asked to print errors to the browser and DBI will not crash a program because of a SQL error (although maybe it should).

If someone does not want their program to die as a result of misusing an API, they can wrap their code in an eval.

Yes, you could return a false or some other error string, but then I have to write each call to your module as $foo->bar() or something; or as $foo->bar(); if $foo->err { something; }. In any case it's going to take just as long to track errors whether you die or try to handle the failure. But why make work for the script(er)? If the programmer absolutely cannot handle a failure they can catch it using $SIG{__DIE__}, I believe (having never felt the need to try this myself, I rely only on what I read at 'perldoc perlvar').

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2020-03-29 18:35 GMT
Find Nodes?
    Voting Booth?
    To "Disagree to disagree" means to:

    Results (171 votes). Check out past polls.