my $path = '/var/www/html/tabulation.html';
my $URL = '' .
'Mike Judge' .
'';
my $tree = HTML::TreeBuilder->new_from_file($path)
or die "Can't open: $!";
$tree->elementify;
foreach my $child ($tree->descendents()) {
# Node doesn't come from a bad section of the tree, right?
# (e.g. head section: titles, already in a href link)
unless (grep { $_ =~ /(head|href)/i }
($child->lineage_tag_names,
$child->all_external_attr_names)) {
my @children = $child->content_list;
my @text_indices = grep { !ref $children[$_] } 0 .. $#children;
foreach my $index (@text_indices) {
my $content = $children[$index];
if ($content =~ /Mike Judge/i) {
$content =~ s/Mike Judge/$URL/ig;
my $literal = HTML::Element->new('~literal','text' => $content);
$child->splice_content($index,1,$literal);
}
}
}
}
print $tree->as_HTML;
$tree->delete;