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. :)

V.Melnik

In reply to Try::Tiny vs. TryCatch: assigning a value to a variable inside of the try-block by v_melnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.