More Ouch
The response to my original Ouch post has been overwhelming. Based upon the number of responses it got and the number of emails I personally received about it, Ouch must have struck a cord.
I've taken the feedback I've received and released Ouch 0.03. New feature details after the jump.
The new features are:
Non-integer codes - You can now use a code like ouch 'missing_param', 'Email address needed.', 'email';
And check for it with kiss 'missing_param'
.
hug
- Catch any exception, including non-ouch exceptions.
use Ouch qw(:traditional)
- Imports four new functions into the namespace. They are:
try
- A nice wrapper aroundeval
.
throw
- An alias forouch
.
catch
- An alias forkiss
.
catch_all
- An alias forhug
.
The usage of these new funcs work like this:
use Ouch qw(:traditional);
my $e = try {
throw 404, 'File not found.';
};
if ( catch 404, $e ) {
# do the big thing
}
elsif ( catch_all $e ) {
# make it stop
}
else {
# make it go
}
Thanks for all the feedback. Enjoy!
Your version of try seems inadequate when compared to that of Try::Tiny.
Would you like to say why? I know that Try::Tiny localizes $@, but I return $@ as $e, so you have it and it won't get clobbered. What else?
I should also mention that there is nothing about Ouch that makes it incompatible with Try::Tiny.
I think he's saying that instead of rolling your own, you should look into tieing Try::Tiny's try() into ouch. :)
Oh, another suggestion:
Maybe a hug_with() function would be neat. Instead of constructing huge honking if/elsif/else blocks i'd prefer to do this:
hug_with(
404 => sub { do_missing_page( $current ) },
'Email address needed.' => \&email_missing,
)
or die_unknown();
@ JT Smith :
Try::Tiny pod explains why it's important to localize $@. The DESTROY example is especially important, as it's more common than on might think. http://search.cpan.org/~doy/Try-Tiny-0.09/lib/Try/Tiny.pm#BACKGROUND
Ouch's implementaion of try() is not good enough for me to use Ouch instead of Try::Tiny. I see why you wanted to improve on Ouch 0.3 based on comments, but for me the real benefit of Ouch is the simplicity. I loved the idea of Ouch, because it didn't follow the traditional try/catch direction. But now it looks like yet another trycatch attempt.
@Damien:
The try implementation in Ouch is just for simplicity sake, for those looking for an easy named wrapper around eval. It is certainly not a primary feature of Ouch. I'm not trying to replicate Try::Tiny in any regard.
In addition, if you look at Ouch 0.04, you'll see that there is a native :trytiny export, which exports more traditional names that are compatible with Try::Tiny. Or if you're like me and don't mind the cute names then you don't even need to export :trytiny, you can simply use Ouch directly with Try::Tiny.