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

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
Tom Molesworth via perl5-porters
Date:
June 17, 2021 00:50
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
CAGXhHdmStHi0YawrTFOt8uzmH4WoiP3U4yTYSz=eVkoeFMe=Tw@mail.gmail.com
On Thu, 17 Jun 2021 at 08:37, Ben Bullock <benkasminbullock@gmail.com>
wrote:

> 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
B::Hooks::EndOfScope.

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


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