Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Style Point: Catching eval { } errors

by Aristotle (Chancellor)
on Nov 26, 2003 at 11:58 UTC ( #310215=note: print w/replies, xml ) Need Help??

in reply to Style Point: Catching eval { } errors

You can even get creative:
if(eval { # Do stuff }, $@) { # Handle any errors }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Style Point: Catching eval { } errors
by dref (Novice) on Nov 28, 2003 at 12:29 UTC
    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.
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://310215]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-03-21 15:00 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (268 votes). Check out past polls.