Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

OK, I admit it, I'm a Perl programmer that has been enamored by many nice things in Ruby, like array arithmetics, several syntax sugars, and the pure OO stuffs. Exceptions is also one of them. When I write small scripts that open or write files, and an I/O error happens, usually failing miserably is my preferred default behaviour. With exception I can be lazy and not handle I/O errors. But when I want to handle some errors, I can start adding try/catch blocks to my code.

Now Fatal and Error are pretty close to what I want, I just wish they can be combined somehow, now that would be gold. (PS: No, Error::SystemException is close but no cigar. I don't want to have to throw exceptions explicitly everytime.)

  • Comment on Advices on doing exception-style programming in Perl?

Replies are listed 'Best First'.
Re: Advices on doing exception-style programming in Perl?
by friedo (Prior) on Nov 19, 2007 at 14:14 UTC
    Block eval is the general way to handle exceptions in Perl, but there are a number of CPAN modules that make it a little prettier and add convenient features. Personally, I like Exception::Class, which lets you define a Java-like exception hierarchy, with the particular advantage that it's not Java.
Re: Advices on doing exception-style programming in Perl?
by TOD (Friar) on Nov 19, 2007 at 13:21 UTC
    perldoc -f eval
    --------------------------------
    masses are the opiate for religion.
Re: Advices on doing exception-style programming in Perl?
by Anonymous Monk on Nov 19, 2007 at 23:26 UTC
    Sorry, bad subject line apparently. I am familiar with eval and the various Exception/Error CPAN modules, it's just that I haven't found the right one to do this: by default make I/O operations (like open, close) throw an I/O exception when failed. There's Fatal which I think is close enough, though I'd like something like that with exception styling to it.