Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: conditional catch-blocks 'try {} catch(COND) { }'

by pryrt (Abbot)
on Sep 20, 2021 at 13:56 UTC ( [id://11136873]=note: print w/replies, xml ) Need Help??


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

>> there's the experimental "Try Catch Exception Handling" introduced in Perl v5.34

> And it's certainly not backwards compatible. Which is a general problem of most experimental features. Having a stable strategy which works with older Perls albeit slowly and nice and fast with newer once would facilitate the development and evalution of new features tremendously.

Which is why there is Feature::Compat::Try (FCT), which will choose between the v5.34 native implementation or the Syntax::Keyword::Try (SKT) implementation, which will mean you can write code in such a way that it is backwards compatible down to the v5.14 that SKT supports, using the same syntax on with newer and older versions of Perl. (If I've understood the docs correctly, SKT actually has additional syntax that isn't in the v5.34 native implementation, but the FCT wrapper only exposes the v5.34-compatible syntax. I think.)

Caveat: I haven't used that pair, because I am often on systems that only has v5.8, so I don't want to get in the habit of using a try/catch pair that's not compatible back that far. But I remembered having seen the FCT/SKT pair mentioned in some other recent thread, so it stuck in my head. So if v5.14 is backward-compatible enough for you, looking at FCT would be a good idea.

  • Comment on Re^3: conditional catch-blocks 'try {} catch(COND) { }'

Replies are listed 'Best First'.
Re^4: conditional catch-blocks 'try {} catch(COND) { }'
by LanX (Saint) on Sep 21, 2021 at 12:29 UTC
    > Syntax::Keyword::Try

    this may be a dumb question, but why is the $e in catch ($e) explicitly needed?

    use Syntax::Keyword::Try; sub foo { try { attempt_a_thing(); return "success"; } catch ($e) { warn "It failed - $e"; return "failure"; } }

    To avoid reusing/redefining a special variable like Try::Tiny does with $_ ?

    We already have $@

    Or is there another reason I'm not aware of?

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

Re^4: conditional catch-blocks 'try {} catch(COND) { }'
by LanX (Saint) on Sep 20, 2021 at 22:41 UTC
    > Which is why there is Feature::Compat::Try (FCT)

    So Syntax::Keyword::Try is an XS module for exactly back-porting this feature "try" till v5.14?

    > So if v5.14 is backward-compatible enough for you, looking at FCT would be a good idea.

    After Devel::Declare and family, I'm no big fan of XS dependencies hacking into the parser.

    The less => the more stable => the better.

    > I haven't used that pair, because I am often on systems that only has v5.8,

    Using pure Perl for backwards compatibility wouldn't have such limitations.

    OTOH using XS would be faster on "older" Perls, at least when parsing.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found