p5h / p5summit-2019

Perl 5 Summit
0 stars 0 forks source link

Perl 6 like Phasers #6

Open toddr opened 4 years ago

toddr commented 4 years ago

Attach blocks to just about anything.

toddr commented 4 years ago

https://docs.perl6.org/language/phasers

leonerd commented 4 years ago

I've often wanted a LEAVE { ... } in perl5; perl6raku's version of it looks fine enough. Far less useful to have an ENTER but for symmetry, why not? I also like the idea that BEGIN et.al. can yield a value - that would be excellent for those one-time constants and other fun things that turn up.

leonerd commented 4 years ago

Another phaser I've always wanted is something like END but that only runs if its line of code was actually reached at runtime. What I often do in the main foo.pl script is something like:

my $thing = Some::Thing->new( shift @ARGV ) or die "Can't get a thing";
$thing->start;
END { $thing->stop }

and then inevitably it crashes at runtime if the or die path is executed, because during that END block the code is still run even though $thing was never set. What I wanted was

END { $thing and $thing->stop }

and it starts to get messy. an END-IF-REACHED { ... } would be nice - though with a better name, obviously :)