Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: Style Point: Catching eval { } errors

by dref (Novice)
on Nov 28, 2003 at 12:29 UTC ( #310710=note: print w/ replies, xml ) Need Help??


in reply to Re: Style Point: Catching eval { } errors
in thread Style Point: Catching eval { } errors

If you want to be creative, and can actually stand the function prototypes, you can do this:

sub try (&@) { my($code, $error_handler) = @_; eval { $code->(); }; $error_handler->() if $@; } sub catch (&) { return @_; }
The & allows you to pass code blocks directly without the sub keyword. You can then write:
my $msg; try { open (FILE, "strangefilename") || die "$!"; $msg = <FILE>; } catch { print "File could not be opened because of: $@\n"; $msg = "BAAAAH"; }
This code is from the perlsub perldoc page.


Comment on Re: Re: Style Point: Catching eval { } errors
Select or Download Code
Re^3: Style Point: Catching eval { } errors
by Aristotle (Chancellor) on Nov 28, 2003 at 15:30 UTC
    This is what Exception lets you do, and unfortunately has numerous problems. The simplest gotcha of all - did you notice your last snippet is missing a semicolon?

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-05 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls