develooper Front page | perl.perl5.porters | Postings from January 2021

Re: Adding a try/catch syntax to Perl

Thread Previous | Thread Next
Paul "LeoNerd" Evans
January 22, 2021 21:40
Re: Adding a try/catch syntax to Perl
Message ID:
On Fri, 22 Jan 2021 20:58:33 +0000
Philip R Brenan <> wrote:

> If we had an LR parser in play that did not require an early
> commitment to the rule to be expanded, instead of an LL one that
> does, we could offer the more expressive:
>   {...}  fail  {...}
> where *fail* is a dyadic operator linking the *try* part to the
> *catch* part.

It turns out that LR grammars are quite brutal on humans as well as
computer parsers.

One of the points of upfront try/catch notation is that it alerts the
reader, when scanning down the code in the usual top-to-bottom fashion,
that something fun is happening with respect to exception handling.
Otherwise, you can be mislead as to the nature of the behaviour if you
get a long piece of code that reads

    ... # 50 lines of code here
  } fail {

once you skip past those 50 lines and realise what was going on.

In the sphere of natural languages there is the concept of a "garden
path sentence"; to quote Wikipedia on the subject:

  A garden-path sentence is a grammatically correct sentence that
  starts in such a way that a reader's most likely interpretation will
  be incorrect; the reader is lured into a parse that turns out to be a
  dead end or yields a clearly unintended meaning. 

"The horse raced past the barn fell." has always been my favourite of
these. As perhaps you can tell by reading it aloud, it causes some
mental upset when you have to reparse what it meant.

I would consider it rude of a language to encourage programmers to
write such programs, even if we can theoretically parse them.

(It is also for this reason I considered but rejected a Raku-style
embedded CATCH {} phaser block inside a regular one.)

Paul "LeoNerd" Evans      |  |

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About