develooper Front page | perl.perl5.porters | Postings from January 2005

Re: [perl #33940] Interrelations of DESTROY and eval are not welldocumented

From:
Aaron Sherman
Date:
January 26, 2005 12:49
Subject:
Re: [perl #33940] Interrelations of DESTROY and eval are not welldocumented
Message ID:
1106772547.32091.195.camel@pps
On Wed, 2005-01-26 at 15:24, Ton Hospel wrote:
> In article <rt-3.0.11-33940-106489.16.0611746958661@perl.org>,
> 	"Victor Porton,,," (via RT) <perlbug-followup@perl.org> writes:
> > I reasonably detailed scanned perlobj, perltoot, perlbot manpages,
> > read "die" and "eval" entries in perlfunc and found no answer.
> > 
> > The questions answer to which seems missing in Perl docs is:
> > 
> > Consider code:
> > 
> > eval {
> >   my $obj1 = Class1->new;
> >   {
> >     my $obj2 = Class2->new;
> >     # Suppose Class2->DESTROY calls 'die "2"'
> >   }
> >   # Suppose Class1->DESTROY also 'die "1"'
> > }
> > 

There are TONS of edge cases (and not so edge cases) around eval/die
semantics. They'll all confuse you at first. The one that bit me hardest
was actually fairly simple:

        $start = time();
        @lines = eval {
                $SIG{ALRM} = sub {die "timeout"};
                alarm(2);
                my $cmd = IO::File->new("sleep 5|");
                $cmd->getlines();
        };
        die $@ if $@ && ($@ !~ /timeout/);
        $end = time();
        $delta = $end-$start;
        print "Got ",scalar(@lines)," answers in $delta seconds.\n";

Of course, this fails (exits in the full 5 seconds) unless you move the
declaration of $cmd outside of the eval so that destruction doesn't
attempt to wait(2) on the child, but that's very non-obvious to someone
who is thinking in terms of "my signal handler said die, so we jump
outside of the eval."

I really wish at times that Perl had a safe eval that you could
absolutely exit from.

-- 
☎ 781-324-3772
✉ ajs@ajs.com
☷ http://www.ajs.com/~ajs




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About