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 19:08
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
CAB=rbOnZiKGG+TSVidV5p=BJE8J2c0jUQYU6SCP2dL23utGTTA@mail.gmail.com
Short summary: CLEANUP is a block's "tail call", it has nothing to do with
any asynchronous event handling (like exceptions).


On Sat, 18 Jul 2020 at 13:23, Felipe Gasper <felipe@felipegasper.com> wrote:

> This assumes that the reader notices the CLEANUP and CATCH code block,
> which isn’t a forgone conclusion given that those blocks exist at the same
> indentation level as the rest. Again, it requires extra diligence to avoid
> mishandling.
>

well, that's also true with nested try blocks ...

Another aspect of CLEANUP is that it has to have access to variables it
cleans, i.e. it has to share the same lexical scope.
For example Java: finally block doesn't see variables declared inside try
block (omitting AutoCloseable resources) so you have to expose its scope
into the upper block anyway so usually you'll end up with wrapping block
anyway.



>
> 2) In large code blocks I could easily miss a CLEANUP by not having
> scrolled up. And oy, what happens if a CLEANUP is declared *midway* through
> such a mess?!? That’s less likely to be problematic with try/catch/finally
> because the indentation guides comprehension of the flow.
>
>
Do you mean something like this? IMHO this code layout is easier to read
and understand than one try with one final containing "if defined"

{
    my $dbh = DBI::connect or die;
    CLEANUP { $dbh->disconnect }

   my $sth = $dbh->prepare ...
   $sth->execute or die;
   CLEANUP { $sth->finish }

   while $sth->fetchrow ...
}

3) Given the overwhelming precedent of try/catch/finally, a new Perl
> developer--likely to come from a different language, maybe resentful of
> having to maintain some dot-com-era code rather than “modernizing” it to
> Python--will more likely grasp try/catch/finally at first glance, whereas
> the more esoteric CLEANUP takes extra study.
>

Well, overwhelming doesn't mean right, there was a time when vast majority
was saying "Sun orbits Earth"


>
> >
> > (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 }
>
> Are there other languages that implement this pattern? How popular are
> these languages?
>

What do you refer to with "this" ?

Writing modules that extend core language? That's quite common in perl, I
believe.

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