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

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

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
July 17, 2020 14:22
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
20200717152207.73f771c9@shy.leonerd.org.uk
On Fri, 17 Jul 2020 10:18:06 -0400
Felipe Gasper <felipe@felipegasper.com> wrote:

> Postscript to this: In my JS work I typically do things like:
> 
> =====
> show_notification('Frobnicating …');
> 
> try {
>     attempt_frobnication();
>     report_success();
> }
> catch (e) {
>     report_failed_frobnication(e);
> }
> finally {
>     hide_notification();
> }
> =====
> 
> … in which context I definitely want the “cleanup” logic to fire
> immediately after the try/catch.
> 
> Without a “standard” finally block I could do it with a scope-guard
> by undef()ing the reference after the catch block. With the proposed
> CLEANUP {} I’d need an extra code block around the try/catch.
> 
> -FG

Would you not write it as

  show_notification('Frobnicating ...');
  CLEANUP { hide_notification() }

  try {
    attempt_frobnification();
    report_success();
  }
  catch ($e) {
    report_failed_frobnification($e);
  }

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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