hi,
firstly , thanks for the help from the previous post.
i am trying to add html tag into html files while traversing a directory.
a simple example is like
my $tree = HTML::TreeBuilder->new;
my $literal = HTML::Element->new('~literal','text' => $insert);
## traverse directory and insert $literal
sub add_html {
$tree->parse_file($file); ## operate on $file
$ele->postinsert($literal); ## add literal
}
$tree->delete;
$literal->delete;
above code won't work unless i put all parts outside the sub into the sub. two
questions :
1. will $tree->parse_file(parse a new file) will overwrite the old parsed $tree
content? so that i
dont have to delete the tree in the sub?
2. defining a static $literal outside the sub, then insert it during the sub. that
will not work
unless i define it inside the sub and delete it later in the sub. why can't i define
it and use it
over and again?
the long program works but looks clumsy and made me curious..
find(\&wanted,".");
sub wanted {
my $file = $_;
return if ($file !~ /\.(html|htm)$/i);
## have to define it everytime ?
my $tree = HTML::TreeBuilder->new; # empty tree
$tree->no_space_compacting(1);
$tree->store_comments(1);
$tree->store_comments(1);
$tree->store_declarations(1);
$tree->parse_file($file);
my $ele;
for (@{ $tree->extract_links('a', 'href') }) {
my($link, $element, $attr, $tag) = @$_;
if ($link=~ m{/events\.htm}) {
$ele = $element;
last;
}
}
unless ($ele) {
$tree->delete;
return;
}
print "-- $file\n";
$ele = $ele->parent(); #td
$ele = $ele->parent(); #tr
## why can't keep it outside ?????????????????????????/
my $literal = HTML::Element->new('~literal','text' => $insert);
$ele->postinsert($literal);
open F,">$file";
print F $tree->as_HTML('<>&',' ',{});
close F;
$tree->delete;
##
$literal->delete;
}
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com