develooper Front page | perl.perl5.porters | Postings from June 2020

RFC: LEAVE phasers

Thread Next
Paul "LeoNerd" Evans
June 27, 2020 12:17
RFC: LEAVE phasers
Message ID:
TL;DR: I want to add LEAVE phaser blocks.

I'm trying to simplify the design of a try/catch syntax out of
Syntax::Keyword::Try so it can be moved into core perl. One thing I
think we can get rid of is the `finally` keyword, in favour of instead
inventing a new `LEAVE` phaser block, similar to the Raku ones.

In summary: I'd like to add `LEAVE` with syntax which looks like
another phaser such as `END`, but runs at the time you leave the block
it is placed in:

  sub f {
    print 1;
    LEAVE { print 3; }

    print 3;


Will output


Furthermore, a LEAVE phaser inside something like a foreach loop would
run every time you leave the block in order to run a new one:

  foreach (qw( a b c )) {
    print "Start($_)";
    LEAVE { print "End($_)"; }

    more code here...

will output


Seems simple enough.

It is important to note that a LEAVE block is similar to an END block,
in that the mere presence of that syntax is enough to queue it for
being run, regardless of whether the line of code it's on was actually
"reached". Thus, just as in

  exit 0;
  END { say "Ending the program now" }

still prints, so too would

  sub f {
    LEAVE { say "Leaving the function now" }

If we had a LEAVE phaser (totally independent of any thoughts of
try/catch), it reduces the scope of that work considerably and makes it
easier to work out how to implement.


Paul "LeoNerd" Evans      |  |

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