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 13:08
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
20200720140846.3eaaec92@shy.leonerd.org.uk
On Sat, 18 Jul 2020 22:06:58 -0400
Felipe Gasper <felipe@felipegasper.com> wrote:

> This aspect seems common to both finally and CLEANUP blocks.

No, they are exactly opposite here. This is the point I was trying to
make.

  try {
    my $new_var = ...
  }
  finally {
    # cannot see $new_var
  }

vs.

  {
    my $new_var = ...
    CLEANUP { $new_var->dispose }  # works just fine
  }

If you wanted to write this in try/finally notation you end up with
another wrapping block around it, and a hoisted variable:

  {
    my $new_var;
    try {
      $new_var = ...
    }
    finally {
      $new_var->dispose;
    }
  }

I much prefer the conciseness of the CLEANUP block.

> What is the advantage in this case of CLEANUP versus Scope::Guard,
> though? Or of putting the teardown logic in a DESTROY() method (as
> DBI does anyway)?
> 
> If we’re not catching errors, then yeah, CLEANUP seems reasonable,
> but in that case it’s just adding a core syntax for Scope::Guard. Is
> that a significant enough thing to warrant being its own feature?

Yes - the entire goal here is to provide most of the basic
functionality as core syntax, and not require a CPAN module. See also
my other mail.

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