bug in HTML::Element::traverse (repost)
Barney Dalton (barneyd@cyllene.uwa.edu.au)
Thu, 7 Jan 1999 08:48:54 +0800 (WST)
I'm reposting this as I got no response, changed the title from problem to
bug in the hope that someone may look at it.
---------- Forwarded message ----------
Date: Tue, 22 Dec 1998 08:55:23 +0800 (WST)
From: Barney Dalton <barneyd@cyllene.uwa.edu.au>
Reply-To: barney@mech.uwa.edu.au
To: libwww-perl@ics.uci.edu
Subject: problem with HTML::Element::traverse
Hi,
I'm having a problem using the traverse function in element.pm. If I do
anything with $_ in my callback function this messes up the HTML tree.
Here's an example:
#!/usr/local/bin/perl
use HTML::TreeBuilder;
$htmltext = <<HTML;
test document
TreeBuilder Test
HTML
$p = HTML::TreeBuilder->new;
$p->parse($htmltext);
$p->traverse(\&process_elements);
print $p->as_HTML();
sub process_elements
{
my $element = shift;
$_="HELLO";
return 1;
}
This prints:
HELLOHELLOHELLOHELLOHELLO
if I change traverse.pm to use a my variable to iterate over the list,
this problem disappears
eg. element.pm is now:
sub traverse
{
my($self, $callback, $ignoretext, $depth) = @_;
$depth ||= 0;
--> my $element; #new my variable instead of $_
if (&$callback($self, 1, $depth)) {
for $element (@{$self->{'_content'}}) {
if (ref $element) { #$element where previously we had $_
$element->traverse($callback, $ignoretext, $depth+1);
} else {
&$callback($element, 1, $depth+1) unless $ignoretext;
}
}
&$callback($self, 0, $depth) unless $emptyElement{$self->{'_tag'}};
}
$self;
}
Is this a bug or a misunderstanding on my part.
barney
/**********************************************************/
/ Barney Dalton /
/ Computer support support@mech.uwa.edu.au /
/ Personal email barney@mech.uwa.edu.au /
/ Work Phone (61) (8) 9380 3058 /
/ Twist my arm : http://telerobot.mech.uwa.edu.au /
/**********************************************************/