develooper Front page | perl.perl5.porters | Postings from June 2016

Re: Confused by eval behavior

Thread Previous | Thread Next
From:
John Siracusa
Date:
June 27, 2016 18:37
Subject:
Re: Confused by eval behavior
Message ID:
CAJu6bqshgoJeRc8Za+OyRSkZ8Ceg8CeMBnucGOTfqBVUrnFB0A@mail.gmail.com
This new example looks even more like a bug:

http://pastebin.com/XtFk7bKs

(Code also appears below.)

-John

#!/usr/local/bin/perl

use strict;
use warnings;

package Dummy;

sub new { bless {}, shift }

sub throw_error {
die 'Died in throw_error';
}

sub DESTROY {
eval { };
}

package DummyContainer;

sub new { bless {}, shift }

sub add_things {
my ($self, $things) = @_;
$things->[0]->throw_error();
}

package main;

use Test::More tests => 1;

eval {
my $dummy = Dummy->new();

my $dummy_container = DummyContainer->new([]);

$dummy_container->add_things([$dummy]);

1;
};

like($@, qr/Died in throw_error/, 'exception thrown in eval is visible');

---

On Fri, Jun 24, 2016 at 3:09 PM, Zefram <zefram@fysh.org> wrote:

> Aristotle Pagaltzis wrote:
> >Does this thread constitute a case where it matters sufficiently to
> >change it?
>
> Yes.  To have a reliable $@ value immediately after eval is important,
> and the programmer can't be expected to anticipate which things might
> be mortalised and so capable of clobbering it.
>
> However, one doesn't necessarily have to solve this by generally changing
> the timing of temp freeing.  Another option is to save and restore $@
> around temp freeing.  It's not obvious which of these would perform
> better.  There may also be some option of changing timing just for eval,
> which if possible would almost certainly have the best performance.
>
> -zefram
>

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