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

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
Tom Molesworth via perl5-porters
June 17, 2021 00:50
Re: RFC 0004 - defer {} syntax
Message ID:
On Thu, 17 Jun 2021 at 08:37, Ben Bullock <>

> I can imagine cases where you might want to clean up after each go around
> of a loop. But I find it quite hard to imagine how it would be useful to
> have a function which runs at the end of an "if" statement block regardless
> of what else happens.

As a generic example:

if($resource->acquire) {
 defer { $resource->release; }
 ... return, exception, next, or similar action here ...
# This line is never reached, so the ->release doesn't happen
# $resource->release;

Would it be useful to make the "defer" statement within an "if" block refer
> to the enclosing function/loop/other block rather than the "if" block, in
> the same way that a "next" statement within the if block refers to the
> enclosing loop?

This would introduce more complexity to the mental model - it's scoped to
the block unless it's an `if/else/elsif/unless` block, but unlike next/last
you can't override the scope with a label - and would take it further away
from the behaviour currently provided by Scope::Guard or

Do you have any examples in mind where this would be a better fit than the
scoped-to-block implementation currently described, though?

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