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

Confused by eval behavior

Thread Next
From:
John Siracusa
Date:
June 23, 2016 16:22
Subject:
Confused by eval behavior
Message ID:
CAJu6bquReNg2KOmhSJ2CpXE3eU6++YHZKhwOhVwQczLz=OCWXA@mail.gmail.com
In light of the 5.14.0 changes to eval/$@ handling:

http://perldoc.perl.org/perl5140delta.html#Exception-Handling

I would expect all the tests in the code below to pass. Instead, two tests
fail in both perl 5.24.0 and 5.16.2 (the only two versions I tested this
on).  Is this expected behavior or a bug? If it is expected behavior, what
in the docs explains it?

The code below also appears at http://pastebin.com/x27PgwXX

-John

---

#!/usr/bin/perl

use strict;
use warnings;

package Foo;

sub new { bless {}, shift }

sub DESTROY {
eval {
die 'Died in destructor';
};

# Ignore $@ here
}

package main;

use Test::More tests => 5;

eval {
my $foo = Foo->new;
my $bar = 123; # this can be any statement, even just "1;"
};

ok(!$@, 'exception handled in destructor (extra final statement in eval)');

eval {
my $foo = Foo->new;
};

ok(!$@, 'exception handled in destructor (no extra final statement in
eval)');

eval {
my $foo = Foo->new;
die 'Died in eval';
};

like($@, qr/^Died in eval/, 'exception thrown in eval (object not destroyed
in last statement)');

eval {
my $foo = Foo->new;
$foo = 123;
die 'Died in eval';
};

like($@, qr/^Died in eval/, 'exception thrown in eval (object destroyed
explicitly, but not in last statement)');

eval {
my $foo = Foo->new;
die ($foo = 'Died in eval');
};

like($@, qr/^Died in eval/, 'exception thrown in eval (object destroyed in
last statement)');

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