Dear Monks,
My knowledge of Perl is admittedly rather basic. I have been truly bewildered by the behaviour of the following piece of code, specifically that "croak" within sub do_something() does not seem to call "die", and that $@ is not set:
#!/usr/bin/perl
package Testing;
use HTML::TreeBuilder;
sub new {
my $class = $_[0];
$class = ref($class) || $class;
my $self = { tree => HTML::TreeBuilder->new() };
bless $self, $class
}
sub DESTROY {
my $self = shift;
eval { $self->{tree} = $self->{tree}->delete(); };
}
sub do_something { Carp::croak "DIE! DIE! DIE!" }
package main;
eval {
my $page = Testing->new();
$page->do_something();
};
if ($@) {
print STDERR "Error: " . $@ if $@;
exit 1;
}
exit 0;
A bit of testing has shown that when eval is used in the destructor of package "Testing", the eval in package "main" does not work, e.g. a croak thrown in a sub inside that eval does not seem to call die(). But why?
The eval in "DESTROY" is merely meant to catch (and ignore) any errors that might arise in that block.
I would be grateful for any pearls of wisdom you may offer.
Update: I've figured out that the destructor mangles $@... I've localized $@ and that fixed my problem.
-- tel
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.