Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by Fletch (Bishop)
on Sep 20, 2021 at 14:10 UTC ( [id://11136874]=note: print w/replies, xml ) Need Help??


in reply to conditional catch-blocks 'try {} catch(COND) { }'

Just kibitzing on your choices, but option one just feels . . . bleh. Can't articulate why but it just doesn't . . . feel right; having the extra filtering sub inside the block . . . meh.

WRT to the other two, while a bit more verbose I like two more than three (which feels (for lack of a better word) like an overly cute hack (and you're also presuming nothing else has used the name _ for a sub)). It strikes me as much less "hah ha look what games I can play with the parser" and more of a "this is just normal perl with two minor magic keywords" (again, my two cents gut feel when I read).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: conditional catch-blocks 'try {} catch(COND) { }'
by LanX (Saint) on Sep 20, 2021 at 16:09 UTC
    I think there are many arguments to be made in favor for using sub _ {} as syntactic sugar, but I forgot one of the most important:

    Chaining sub {} requires commas as separators!

    Please compare

    use strict; use warnings; sub try (&;@) {}; sub catch (&;@){}; sub _ (&;@) {}; sub error_classes { return map { $_ => bless {}, $_ } @_; } use constant { error_classes qw( FileNotFoundError IsADirectoryError ) }; try { # your code here } catch { FileNotFoundError } sub { # handle exception }, # extra comma needed catch { IsADirectoryError } sub { # handle exception }, catch { # all other types of exceptions }; warn ('Should reach here'); try { # your code here } catch { FileNotFoundError }_{ # handle exception } catch { IsADirectoryError }_{ # handle exception } catch { # all other types of exceptions }; warn('Should reach here')

    Hence the OP was wrong and needs to be corrected.

    If anyone sees a way to avoid the trailing commas (or fat commas), please feel free to show ...

    Otherwise, this break in symmetry is just "meh" ...

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

      Ah, valid point. Still slightly wary of . . . overloading the single _ with syntactic meaning but it does at least look cleaner than the properly comma'd code. Objection weakened at the least.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found