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

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
Dan Book
Date:
June 18, 2021 16:35
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
CABMkAVXy6XjmUYA9bscMFGyyj9ucE_Yx6SCh=z5V54+wiYyqRg@mail.gmail.com
On Fri, Jun 18, 2021 at 12:28 PM Eirik Berg Hanssen <
Eirik-Berg.Hanssen@allverden.no> wrote:

>
>
> On Fri, Jun 18, 2021 at 6:08 PM David Nicol <davidnicol@gmail.com> wrote:
>
>>
>>
>> so what's wrong with just squidding it out in long form?
>>
>>    sub abcdefg {
>>        my $defer;
>>        ...
>>        my_predicate and $defer = sub {warn "MY_PREDICATE WUZ TROO!\n"};
>>        ...
>>        $defer and &$defer;
>>        $retval;
>>    }
>>
>
>   That won't run if scope is exited during the second ellipsis.
>
>   Whereas the defer "enqueues a block of code for later execution, when
> control leaves its surrounding scope for whatever reason", so as long as
> the defer statement is reached, the block will eventually run.  (Well,
> untimely program termination will prevent it, I guess.)
>

Yes, the primary usecase for scope guards is to run regardless of whether
the scope is normally exited. Abnormal scope exits include exceptions, loop
control flow, and returning from a subroutine, among more esoteric options.

-Dan

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