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

Re: try/catch and CLEANUP/FINALLY/&c.

Thread Previous | Thread Next
From:
=?UTF-8?Q?Branislav_Zahradn=C3=ADk?=
Date:
July 18, 2020 07:01
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
CAB=rbOnfmqsLuThs9T1rBUgiK_hcQKeWVzHjq168ZRaqT9Txzw@mail.gmail.com
On Fri, 17 Jul 2020 at 16:30, Felipe Gasper <felipe@felipegasper.com> wrote:

>
>
> > On Jul 17, 2020, at 10:22 AM, Paul LeoNerd Evans <leonerd@leonerd.org.uk>
> wrote:
> >
> > On Fri, 17 Jul 2020 10:18:06 -0400
>
>
> Possibly, but then if I or someone else adds logic after the catch block,
> the CLEANUP won’t fire until after that. Consider:
>
> =====
> show_notification('Frobnicating ...');
> CLEANUP { hide_notification() }
>
> try {
>   attempt_frobnification();
>   report_success();
> }
> catch ($e) {
>   report_failed_frobnification($e);
> }
>
> blocks_for_10_seconds();
>
>
In such case you are free to use explicit block (using lexical-catch
variant) (you should wrap that into  dedicated method anyway)

show_notification(...);
{
    CLEANUP { hide_notification };
    CATCH { report_failed... }

    atempt_...
    report_success
}
block_for_10_seconds

Lexical cleanup and catch blocks are more powerful than just plain
try/catch/finally,

Ability to specify actions on exit before actual exit happens notifies the
reader (maintainer after 10 years ...)
that there is something "before" its eyes catch some exit point.

Plain cleanup is also a little bit less confusing than try/finally
construct (there is try, where is catch?)

(imho less powerful) try/catch/finally syntax can be still introduced by
external module with rewriting:
   try TRYBLOCK catch CATCHBLOCK finally FINALLYBLOCK
into
  { CATCH CATCHBLOCK  CLEANUP FINALLYBLOCK TRYBLOCK }

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