Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Exception handling (alternatives)

by danger (Priest)
on Dec 21, 2001 at 13:20 UTC ( [id://133728]=note: print w/replies, xml ) Need Help??


in reply to Exception handling (alternatives)

From your code:
# (by the way, i _do_ hate having to see # that 'sub' word right after try... # since there's no other way to # pass a block of code as an argument other than # via the anonymous sub/closure) # # try sub { # foo(); # };

You can use prototypes to enable passing sub-refs without the 'sub' keyword. See, for example, the perlsub manpage, and/or some of the other exception handling modules:Exception::Class, Error, and the older Exception for variations on try{}catch{} examples (actually, Exception::Class doesn't implement try/catch directly but you can use it in conjunction with Error.pm (according to the docs) to get that syntax with it). The (&) prototype is one of the few useful uses of prototypes (in certain circumstances).

Replies are listed 'Best First'.
Re: Re: Exception handling (alternatives)
by vladb (Vicar) on Dec 21, 2001 at 19:38 UTC
    Great suggestion there! ;-).

    I'll take a look. I sort of suspected that there should have been something to help me out in that case (to eliminate the need of using 'sub' word).



    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
      Here's what I've been able to dig up by running perldoc perlsub. This piece of code is exactly to the point in my case. Again, thanks for pointing me in the right direction! ;-) cheers.
      sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { local $_ = $@; &$catch; } } sub catch (&) { $_[0] } try { die "phooey"; } catch { /phooey/ and print "unphooey\n"; };


      "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found