more useful options | |
PerlMonks |
conditional catch-blocks 'try {} catch(COND) { }'by LanX (Saint) |
on Sep 19, 2021 at 20:30 UTC ( [id://11136862]=perlquestion: print w/replies, xml ) | Need Help?? |
LanX has asked for the wisdom of the Perl Monks concerning the following question:
Hi this is part of a longer meditation, but I want to keep it short: I want to be able to catch error-objects thrown with die and I want to have an easy syntax: the JS model is reproducible 1-to-1 with Try::Tiny it's even easier because Perl's $_ is automatically set to JS's e from try...catch#conditional_catch-blocks
But in most cases the final else will just do die $_ to propagate the error to higher call levels, hence it's boilerplate. Python has a model for this by providing a condition after the 'catch', if non is matched the error is raised again. from https://pythonbasics.org/try-except/
(* the last except must be removed to automatically raise the error again) So ideally one could define in Perl a prototype for catch (COND) { CODE } where the (COND) part is optional. Alas that's not possible in Perl, even with mandatory (COND) (otherwise experiments with syntax extension were easy) Three workarounds come into mind
1. a bail out function only {} (or maybe handle {} )
(* again, if the simple catch is removed the error is automatically thrown again die $_ ) ( ° the COND is either a boolean expression or a constant resp. object representing an error-class. Since an error-class is blessed into a type like "ErrorClass" this can be tested by only ... hence a shorthand for $_->isa(FileNotFoundError) ... not sure if Python has the same flexibility ;)
2. extend catch (&;&) {...} with optional second sub {}
UPDATE: added commas, plz see here why.
3. as a variant of 2. use an "underscore" sub _ as syntactic sugar to chain code-blockswith sub _ (&;@){ return @_ }
This variant could help implementing a future built-in syntax catch () {} by parsing {...}_ as (...) Like that module authors wanting to be backwards compatible to older versions could keep writing {...}_ without loosing the full performance of newer versions. And new syntax for compound statements could be experimentally implemented and tested.
finallyI wanted to keep it short and didn't show much implementation detail. I'm more interested in comments regarding the interface... or probably I'm missing a good CPAN module already? comments? suggestions?
Cheers Rolf
Back to
Seekers of Perl Wisdom
|
|