Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: Use of "die" in OO modules

by adrianh (Chancellor)
on Jan 27, 2007 at 21:04 UTC ( #596904=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Use of "die" in OO modules
in thread Use of "die" in OO modules

but it is not really much of an object (at least, not in the sense that it's commonly used and understood by practitioners of Object-Oriented paradigm

Well - you get as much of an object as you give it. If you die with a string you get a string. If you die with an object you get an object. We don't have a specialised exception class - but that's the way Perl 5 is in general :)


Comment on Re^5: Use of "die" in OO modules
Re^6: Use of "die" in OO modules
by theguvnor (Chaplain) on Jan 27, 2007 at 23:59 UTC
    Are you saying that if I eval { die MyExceptionClass->new(...) }; that the $@ variable will contain the instance of MyExceptionClass? Wow. It never dawned on me that that might be the case... This changes everything!

    [Jon]

      Are you saying that if I eval { die MyExceptionClass->new(...) }; that the $@ variable will contain the instance of MyExceptionClass?

      Exactly ;-) From perldoc die:

      die() can also be called with a reference argument. If this happens to be trapped within an eval(), $@ contains the reference. This behavior permits a more elaborate exception handling implementation using objects that maintain arbitrary state about the nature of the exception. Such a scheme is sometimes preferable to matching particular string values of $@ using regular expressions.

        Awesome.. I tried it and I like it!

        Now I just wish that something like this:

        catch (ThisClass $e) {...} catch (ThatClass $e) {...}
        was built into Perl!!

        [Jon]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-12-28 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls