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

V.Melnik

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

    Hello v_melnink,

    Update: I did not see Athanasius reply. Possibly is something that you are looking for.

    I am not really sure what you are trying to achieve but a few points that I have noticed.

    Alas, we can't do it with TryCatch,

    Are you referring to Try::Tiny or to Error::TryCatch?

    I can not understand what you are trying to achieve so I created a small sample of code that probably it will not help you but probably can help you describe what you are truing to do.

    #!/usr/bin/perl use strict; use warnings; use Try::Tiny; sub normal { open my $normal , ">" , "normal.txt" or die $!; print $normal "This is just a sample\n"; close $normal or die $!; } normal(); sub handle { open my $handle , ">" , "handle.txt"; my $open = try { die $!; } catch { warn "caught error: $_"; # not $@ }; print "\$open: ".$open."\n"; print $handle "This is another sample\n"; close $handle; my $close = try { die $!; } catch { warn "caught error: $_"; # not $@ }; print "\$close: ".$close."\n"; } handle(); __END__ caught error: Inappropriate ioctl for device at try::tint.pl line 22. $open: 1 caught error: Inappropriate ioctl for device at try::tint.pl line 33. $close: 1

    Take a look also on alternative modules, Error::TryCatch and Error. Maybe you fill find some functionality that you where looking for.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      "Are you referring to Try::Tiny or to Error::TryCatch?"

      No... TryCatch.

      It's pretty awesome, but a little slow, and very heavy on the dependencies.

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

    With regard to choosing the catch block based on the class of the exception thrown (I presume you are using something like Class::Exception or Throwable) have you considered a dispatch table?

    # this is a very crude concept example my $val = try { some_operation(); } catch { if ( blessed($_) ) { &{$exceptions_by_class{blessed($_)}}($_); } else { # handle normal die or croak message ... } };

    I must confess that I am a big fan of Try::Tiny and have not played with TryCatch.

    Update: fixed typo in code.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.