in reply to Re^3: conditional catch-blocks 'try {} catch(COND) { }'
in thread conditional catch-blocks 'try {} catch(COND) { }'

A little bit more code and indenting than expected, but yes, that should work.

Try::Tiny implements catch as a prototyped sub (sub catch (&;@)), so it should be possible to implement something like sub catchif ($&;@) that accepts an additional parameter, and to extend the logic in sub try to basically implement your handle_error() function.

That would allow the following syntax that at least looks similar to try-catch in other languages:

try { # ... } catchif TypeError => { # ... } catchif RangeError => { # ... } catch { # ... } finally { # ... }

I think it would not need many changes:

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^5: conditional catch-blocks 'try {} catch(COND) { }'
by LanX (Saint) on Sep 21, 2021 at 11:39 UTC
    > That would allow the following syntax that at least looks similar to try-catch in other languages:
    try { # ... } catchif TypeError => { # ... } catchif RangeError => { # ... } catch { ...

    May I see a POC with prototypes for that?

    Because this

    > sub catchif ($&;@)

    will require a sub before and a comma after the block.

    try { # ... } catchif TypeError => sub { # ... }, catchif RangeError => sub { # ... }, catch { ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      May I see a POC with prototypes for that?

      Because this

      > sub catchif ($&;@)

      will require a sub before and a comma after the block.

      You are right. It would have been too easy. ;-) perlsub is quite clear:

      An & requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma.

      I did not remember the "first argument" exception and assumed an anonymous sub would work in any position.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        > assumed an anonymous sub would work in any position.

        Oh that would be so nice...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery