Skip to content

Adding a try/catch syntax to Perl #18504

@leonerd

Description

@leonerd

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 - 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 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 :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions