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

Adding a try/catch syntax to Perl

Thread Next
From:
Paul "LeoNerd" Evans
Date:
January 22, 2021 17:02
Subject:
Adding a try/catch syntax to Perl
Message ID:
20210122170239.2421d3fd@shy.leonerd.org.uk
I intend to add try/catch syntax. This has been one of the most
frequently-requested of core syntax features - from both community
sources like Freenode #perl and other core developers - so I do not
anticipate this will be a contentious or debated idea.

As many of the existing CPAN solutions all follow a very similar theme
- including my own Syntax::Keyword::Try[1] - I expect I shall follow
that pattern, and likewise do not anticipate that this will be
considered controversial. Syntax::Keyword::Try has been the preferred
module by Freenode #perl for a while now, and folks seem happy with it.

I further intend it to be introduced as

  use feature 'try';

Towards this goal, I have created a module Feature::Compat::Try[2] to
simplify migration. Existing authors can

  use Feature::Compat::Try;

in their current code, today, and import a configured version of
Syntax::Keyword::Try which works on all Perl versions back to 5.16, and
conforms exactly to the specification of the thing I want to add to
core. (S:K:T additionally contains other features, experimental ideas,
and past mistakes, that I do not intend to bring in at this time, hence
the configuration).

A brief summary, from its synopsis, is:

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

If anyone wishes to review in more detail the syntax and semantics,
please read the documentation and unit-tests of this module. That is
what I intend to implement exactly. You do not have to wait until I
have a PR to actually implement it to do this - the earlier you do
this, the better :)


1: https://metacpan.org/pod/Syntax::Keyword::Try

2: https://metacpan.org/pod/Feature::Compat::Try

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About