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

Re: Review of `defer` (was: FINALLY - ready for comments/review)

Thread Previous | Thread Next
From:
demerphq
Date:
August 6, 2021 09:50
Subject:
Re: Review of `defer` (was: FINALLY - ready for comments/review)
Message ID:
CANgJU+Vw=fvE+VRwqn0d550cSjUGVDhV_Zz_ktHWLhcwggioMA@mail.gmail.com
On Fri, 6 Aug 2021 at 09:47, Nicholas Clark <nick@ccl4.org> wrote:

> On Tue, Aug 03, 2021 at 05:58:35PM +0100, Paul "LeoNerd" Evans wrote:
> > On Wed, 28 Jul 2021 14:00:46 +0100
> > "Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:
> >
> > > The branch for core perl now lives at:
> > >
> > >    https://github.com/leonerd/perl5/tree/defer
> >
> > There not being any show-stopping complaints thus far, I have now
> > opened an actual PR for this branch:
> >
> >   https://github.com/Perl/perl5/pull/19019
>
> I agree that there aren't any show stoppers.
>
> I did raise a couple of questions in the previous thread that seem to have
> got missed (so don't have answers):
>
> https://www.nntp.perl.org/group/perl.perl5.porters/2021/07/msg260747.html
>
> The one that bugs me most is:
>
>
> I think that this would be the first time in perl that the behaviour
> between
> `eval` and not eval differs. In that without eval:
>
> $ ./perl -Ilib -E 'use feature "defer"; sub foo { defer { die "One"; }
> defer { die "Two" } say "Hi"}; foo(); say "Bye"'
> defer is experimental at -e line 1.
> defer is experimental at -e line 1.
> Hi
> Two at -e line 1.
> One at -e line 1.
>
>
> We get both exceptions to STDERR and then the process exits:
>
>
> $ ./perl -Ilib -E 'use feature "defer"; sub foo { defer { die "One"; }
> defer { die "Two" } say "Hi"}; eval { foo() }; say "Bye"'
> defer is experimental at -e line 1.
> defer is experimental at -e line 1.
> Hi
> Bye
>
>
>
> whereas $@ can only capture one:
>
> $ ./perl -Ilib -E 'use feature "defer"; sub foo { defer { die "One"; }
> defer { die "Two" } say "Hi"}; eval { foo() }; say "\$@ is $@"; say "Bye"'
> defer is experimental at -e line 1.
> defer is experimental at -e line 1.
> Hi
> $@ is One at -e line 1.
>
> Bye
> $
>
>
> and if the block exit is caused by an exception:
>
> $ ./perl -Ilib -E 'use feature "defer"; sub foo { defer { die "One"; }
> defer { die "Two" } die "Three"}; eval { foo() }; say "\$@ is $@"; say
> "Bye"'
> defer is experimental at -e line 1.
> defer is experimental at -e line 1.
> $@ is One at -e line 1.
>
> Bye
> $ ./perl -Ilib -E 'use feature "defer"; sub foo { defer { die "One"; }
> defer { die "Two" } die "Three"}; foo(); say "\$@ is $@"; say "Bye"'
> defer is experimental at -e line 1.
> defer is experimental at -e line 1.
> Three at -e line 1.
> Two at -e line 1.
> One at -e line 1.
>
>
> it's consistent - the last error encountered is in $@. But errors get
> eaten.
>

If one used a $SIG{__DIE__} one would have access to all the errors as they
occured, or I would expect I would.


>
> I'm struggling to suggest a better solution. I *think* that possibly $@
> ought to be the *first* error encountered. As Klortho glibly put it:
>
> #11916 Always ignore the second error message unless the meaning is obvious
>
> but what's bugging me is that the second (or subsequent errors) might be
> fallout from the problem that caused the first exception and error message,
> but if that error message is lost (and not reported) it could be rather
> hard
> to figure out that actual problem.
>
>
> Do we also need to create a variable @@ which exposes the stack of
> exceptions
> encountered? (With $@ set to one of them, last as you currently have it,
> first as I'm suggesting, and unchanged for existing problematic situations,
> such as exceptions firing in DESTROY)
>

So an internal version of:

  my @errors;
  local $SIG{__DIE__}= sub { push @errors, @_; die @_};

Sounds useful, especially as it would leave __DIE__ handler chaining out of
the question.

cheers,
Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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