James.Q.L wrote:
>
> Thurn, Martin wrote:
> >
> > I ran into similar problems for my module WWW::Search.
> > No, out-of-the-box you can not re-use an HTML::TreeBuilder object to parse
> > a new file. BUT you can use the following code as a "reset". I.e. call
> > parse, muck with the tree, do the following four lines, and call parse
> > again. This does the same as new() but without changing the store_comments,
> > store_pis settings, etc:
> >
> > $self->{'_head'} = $self->insert_element('head',1);
> > $self->{'_pos'} = undef; # pull it back up
> > $self->{'_body'} = $self->insert_element('body',1);
> > $self->{'_pos'} = undef; # pull it back up again
>
> i will look into how that can be added to $tree later.
> but a reset method from HTML::TreeBuilder would be nice.
As per my previous post, I think $tree->eof or $tree->delete_content will
do the trick.
> > The reason you can't re-use your HTML::Element is because it's a
> > reference, and when the tree gets deleted, your Element gets deleted right
> > along with it.
> >
> > - - Martin Thurn
> >
>
> just found that $h->clone can get around this. too bad i can't do the same to a tree
> before it
> parses the file.
>
> from HTML::Element,
>
> You are free to clone HTML::TreeBuilder trees, just as long as: 1) they're done
> being parsed, or
> 2) you don't expect to resume parsing into the clone. (You can continue parsing into
> the original;
> it is never affected.)
>
> just to make sure, (english isn't my first language) does it say that i can't clone
> a tree if it
> doesn't parse something?
No. What this is saying is that you can't do:
my $tree = new HTML::TreeBuilder;
$tree->parse($string1);
$tree->parse($string2);
my $tree2 = $tree->clone;
$tree2->parse($string3);
But, here, you could write
$tree->parse($string4);
In other words, you can't carry on parsing HTML text into a cloned
tree, but the original tree is unaffected.
HTH,
Rob