casimo has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to parse some html using HTML::TreeBuilder but it is hanging on big pages.

is there a way to get the following code to timeout after x seconds?

$p = HTML::TreeBuilder->new; $p->parse($content);

Replies are listed 'Best First'.
Re: Parsing with HTML::TreeBuilder hangs
by GrandFather (Saint) on Mar 02, 2010 at 03:38 UTC

    You could:

    $SIG{ALRM} = sub{die "too long"}; my $xSecs = 1; my $content = ""; eval { alarm $xSecs; my $p = HTML::TreeBuilder->new (); $p->parse($content); alarm 0; }; print "Died after waiting $xSecs" if $@ =~ /^too long/;

    True laziness is hard work