develooper Front page | perl.perl5.porters | Postings from October 2021

Re: Pre-RFC: try/catch/finally and generic finally blocks

Thread Previous | Thread Next
From:
Oodler 577 via perl5-porters
Date:
October 20, 2021 04:57
Subject:
Re: Pre-RFC: try/catch/finally and generic finally blocks
Message ID:
YW+hunU/p9XUe8YN@odin.sdf-eu.org
* Dan Book <grinnz@gmail.com> [2021-10-19 20:48:15 -0400]:

> On Tue, Oct 19, 2021 at 7:45 PM Oodler 577 via perl5-porters <
> perl5-porters@perl.org> wrote:
> 
> > * hv@crypt.org <hv@crypt.org> [2021-10-19 21:49:47 +0100]:
> >
> > > "Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:
> > > [...]
> > > :  try {
> > > :    ## 100 lines here
> > > :  }
> > > :  finally {
> > > :    say "Oh and just do this before you go"
> > > :  }
> > > [...]
> > > :  {
> > > :    ## 100 lines here
> > > :  }
> > > :  finally {
> > > :    say "Oh and just do this before you go"
> > > :  }
> >
> > Replying to Hugo's comment only to honor it; but in the case of
> > the bare block this seems like a NOOP unless "finally" implies a
> > "wait" or waitpid for thing, but I don't know what things that
> > could be other than a child process spawned by either C<fork> or
> > an explict background shell command via C<system> or C<``> or
> > C<qx//>, etc.
> >
> > LABEL:
> > {
> >  #  do stuff
> >  # spawn a child proc
> > }
> > finally {
> >  # do more stuff but what's
> >  # the state of child process?
> > }
> >
> > 1. when does `finally` get executed, does it wait for child pid?
> > 2. does LABEL: work as expected if one does, `next LABEL;`?
> >
> 
> Forking has no relevance to the concept. The difference finally makes, as
> with defer, is that it runs when the associated scope is exited, even if
> it's done so via an exception, loop control, etc.

I guess I don't understand the value of providing this for a bare block,
and the only value I can see in any other block structure is to extend
scope "one more time".

    # currently
    my $i;
    for $i (1..10) {
      # do stuff
    }
    if ($i < 10) {
      print qq{Loop exited early!\n};
    }

versus

    # with 'finally'
    for my $i (1..10) {
      # do stuff
    }
    finally {
      if ($i < 10) {
        print qq{Loop exited early!\n};
      }
    }

Similarly, I see value in extending this to the block form of eval,

    local $@;
    my $ret = eval {
      my $inner_var = q{asdf};
      #... do stuf that might die
    }
    finally {
      print qq{$inner_var};
    }
    
    #$@ and $ret available here, $inner_var is not
    #...

It also seems to provide the corollary to do { .. } while (), which provides
an in scope "pre step" versus a basic while () { ... }.

So maybe to be consist, the "finally" on a "while" should be like,

    while (...) {
    
    }
    do {
    
    }

Or maybe, "finally" should just be "done" for all,

    while (...) {
    
    }
    done {
    
    }

or
    
    try  {
    
    }
    catch {
    
    }
    done {
    
    }

or
    
    for my $i (1..10) {
    
    }
    done {
    
    }

etc

Thanks,
Brett

> 
> -Dan

-- 
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native

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