recognizing and removing an entity from a text node with HTML::Tree

Terrence Brannon <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
When I set $target in the following program to an obvious string, I can
remove it from any text segment with ease. What I am confused about it why I
cannot set $target to &nbsp; and then remove it just as easily.

use strict;
use warnings;
use HTML::TreeBuilder;

my $html = '<h1>Blah</h1>&nbsp;hithere&nbsp;<p>&nbsp;<br>
<h2>Blah</h2>';

my $element_root = HTML::TreeBuilder->new_from_content($html);

$element_root->objectify_text;

$element_root->dump;
print "\n";

my $target;
$target = '&nbsp;';
$target = 'hi';

my @text = $element_root->look_down('_tag' => '~text');
for my $text_node (@text) {
  my $tmp = $text_node->attr('text');
  warn $tmp;
  if ($tmp =~ m!$target!) {
    warn 'here';
    $tmp =~ s!$target!!g;
    $text_node->attr(text => $tmp);
  }
}

print "\n";
$element_root->dump;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.