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

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

Thread Previous | Thread Next
Paul "LeoNerd" Evans
July 20, 2020 13:01
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
On Sat, 18 Jul 2020 10:16:04 -0400
Felipe Gasper <> wrote:

> >   Without a catch, this follows the same pattern as Perl's END
> > blocks, no?
> > 
> >   Seems less confusing to this Perl programmer, at least.  
> That’s fair. But then, what gain is realized from CLEANUP versus the
> likes of Scope::Guard? A bit less typing, I guess?

Yes; that is basically it. This, plus bringing it into the language as
a real core-supported feature.

It isn't just Scope::Guard; that module's SEE ALSO section also lists
quite a few others:

  * B::Hooks::EndOfScope
  * End
  * Guard
  * Hook::Scope
  * Object::Destroyer
  * Perl::AtEndOfScope
  * ReleaseAction
  * Scope::local_OnExit
  * Scope::OnExit
  * Sub::ScopeFinalizer
  * Value::Canary

The fact that there are so many of these things on CPAN already
provides me a signal (as similarly it does for try/catch, type systems,
object systems, ...) that core language should be doing something about

The idea for a CLEANUP[1] syntax in core perl isn't to provide the
fanciest, best, most capable system ever - trying to cover all the
bases would inevitably lead to something so complex as to be hard to
implement, hard to explain, and unpredictable to use.

The goal here - as with all core language design - is to try to find
the right balance between power, flexibility, and simplicity. In this
case, I feel that the ease of explaining (and implementing) the simple
CLEANUP syntax is sufficient to say it can replace a good 80-90% of the
use-cases of Scope::Guard and all the other modules listed above. For
the rarer other cases where it isn't sufficient (e.g. conditional
cleanup, or pushing out to parent scopes), you can of course still use
one of the existing CPAN modules. Nothing in this idea takes away from


[1]: I am further leaning to naming it FINALLY actually, because it
  does closely follow the semantics of try/finally syntax in those
  languages which provide it.

Paul "LeoNerd" Evans      |  |

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About