v_melnik has asked for the wisdom of the Perl Monks concerning the following question:
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. :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block
by Athanasius (Bishop) on Oct 08, 2014 at 15:07 UTC | |
Re: Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block
by tobyink (Canon) on Oct 08, 2014 at 21:18 UTC | |
Re: Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block
by thanos1983 (Parson) on Oct 08, 2014 at 15:19 UTC | |
by tobyink (Canon) on Oct 08, 2014 at 18:31 UTC | |
Re: Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block
by boftx (Deacon) on Oct 09, 2014 at 05:56 UTC |