I almost have no hope that there is a way to achieve what I want, but I'd better ask more experienced colleagues than make a mistake.
I like Try::Tiny very much, because it makes possible to do so something like that:
my $result = try { SomeClass->new } catch { warn };
Alas, we can't do it with TryCatch, because it doesn't return the result the try-block, so TryCatch forces us to do something like that:
my $result; try { $result = SomeClass->new } catch { warn };
As for me, TryCatch is better, because it allows us to control the flow by return/next/last and other commands and it can choose the catch-block depending on the class of the exception (I really love this feature!), but the only thing I can't bear with - that I must declare the resulting variable by a separate my-command before to use try.
What do you think, is there any ways to make the try-block to return a value?
Thank you!
UPD:Lots of thanks for all ideas. I decided to keep using TryCatch, but I'll declare all variables in the beginning of the block:
sub some_sub { my $foo; my $bar; my $baz; # ... try { $foo = SomeClass->new; } # ... try { $bar = $foo->old; } # ... try { $baz = $bar->rotten; } # ... }
I think, declaring all variables in the beginning of a block is a pretty good manner. Some of languages are quite strict about that, so I can just stop indulging myself with declaring them wherever I want. :)
In reply to Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block by v_melnik
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |