On Wed Jul 02 14:36:31 2014, MITHALDU wrote:
Show quoted text> I'd like to be able to define catches like this:
>
> use Try::Tiny::ByClass;
>
> try {
> die $exception_object;
> }
> catch_case [
> 'Some::Class' => sub { }, # handle Some::Class exceptions
> 'Exception::DivByZero' => sub { }, # handle Exception::DivByZero
> exceptions
> 'MyExceptionLib' => [
> "DivByZero' => sub { }, # handle MyExceptionLib::DivByZero
> exceptions
> "FileOpen' => sub { }, # handle MyExceptionLib::FileOpen
> exceptions
> "User::Invalid' => sub { }, # handle
> MyExceptionLib::User::Invalid exceptions
> ],
> ],
> finally {
> # always do this
> };
This looks like it could be done with a helper function:
sub with_prefix {
my $prefix = shift;
my @ret;
while (my ($k, $v) = splice @_, 0, 2) {
push @ret, "${prefix}::$k", $v;
}
@ret
}
try {}
catch_case [
'Some::Class' => sub { }, # handle Some::Class exceptions
'Exception::DivByZero' => sub { }, # handle Exception::DivByZero exceptions
with_prefix('MyExceptionLib' =>
"DivByZero' => sub { }, # handle MyExceptionLib::DivByZero exceptions
"FileOpen' => sub { }, # handle MyExceptionLib::FileOpen exceptions
"User::Invalid' => sub { }, # handle MyExceptionLib::User::Invalid exceptions
),
];
I could include such a helper function, but it doesn't really have anything to do with exceptions; it's just data transformation. So I'm not sure it belongs here.