Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^11: eval to replace die?

by BrowserUk (Pope)
on Oct 05, 2010 at 02:02 UTC ( #863492=note: print w/ replies, xml ) Need Help??


in reply to Re^10: eval to replace die?
in thread eval to replace die?

the unreliable part is writing good regular expressions which are not fragile in the face of simple changes to unstructured data

Ah! Now we're getting some where. I'm going to assume that you don;t consider yourself incapable of writing a good regex; which by implication suggests that you assume that wonderful catch-all, "other people", can't.

There's a really simple solution to that: regularise your error messages.

You're already advocating moving your exceptions into a single file--the Exception::Class using exception declaration module.

So, do the same thing with your error messages. And make them easy to parse:

package MyModule::Errors; use constant { ERROR001 => 'MyModule::Error001: The frobwitz has a %d chance of r +eaching critcial mass", ERROR002 => 'MyModule::Error002: The doobry '%s' is frazzled', ... }; 1;

Now all you need is:

eval { somefunc() ); if( $@ =~ m[^MyModule::Error002] ) { ... };

Simple. Lightweight. Reliable.

Of course, now we're well on the way to returning error numbers instead of strings. And then providing a function (class method) that converts them to an error text (when required). With the added benefit of making I8N simple.

Don't cover up simple design issues by layering heavy & complicated code over the top. Design a solution that addresses those issues at source. In this case, the source is the string form of the error returns, not the regexes the user needs to use to handle them.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^11: eval to replace die?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-20 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls