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 20, 2020 14:03
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
20200720150312.70fc29d4@shy.leonerd.org.uk
On Mon, 20 Jul 2020 09:33:48 -0400
Felipe Gasper <felipe@felipegasper.com> wrote:

> As a replacement for Scope::Guard, FINALLY{} makes a lot of sense,
> and your point about how widely implemented the FINALLY pattern is on
> CPAN is well-taken. It does seem a bit redundant with DESTROY blocks,
> at least in the case of bless()ed references, so assuming this lands
> it might be nice to compare those two forms of tail logic in the docs.

Plus, the implementation is independent of DESTROY and reference
counting on lexicals, being guaranteed by other mechanisms. Moving to a
world in which fewer people rely on reliable object destruction has
other benefits too... ;)

> As to obviating stuff like Try::Catch, though … is the idea, then,
> that we’d do something the likes of this?
> 
> =====
>     my $thing = My::Thing->new();
> 
>     FINALLY { $thing->clean_up() }
> 
>     eval { $thing->frobnicate(); 1 } or do {
>         if ($@ isa 'X::Error1') { .. }
>         elsif ($@ isa 'X::Error2') { .. }
>         else { die }
>     };
> =====

Not really. The end-goal here includes having real try/catch syntax in
core. I'd have

  my $thing = My::Thing->new;
  FINALLY { $thing->clean_up; }

  try {
    $thing->frobnicate;
  }
  catch ($e isa X::Error1) { .. }
  catch ($e isa X::Error2) { .. }

But that's a whole new discussion. This FINALLY block is just the first
step.

-- 
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