in reply to Re: Style Point: Catching eval { } errors
in thread Style Point: Catching eval { } errors
The & allows you to pass code blocks directly without the sub keyword. You can then write:sub try (&@) { my($code, $error_handler) = @_; eval { $code->(); }; $error_handler->() if $@; } sub catch (&) { return @_; }
This code is from the perlsub perldoc page.my $msg; try { open (FILE, "strangefilename") || die "$!"; $msg = <FILE>; } catch { print "File could not be opened because of: $@\n"; $msg = "BAAAAH"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Style Point: Catching eval { } errors
by Aristotle (Chancellor) on Nov 28, 2003 at 15:30 UTC |