http://www.perlmonks.org?node_id=942838

cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I have a script running on a server that is getting and parsing Google alerts. My question refers to the following sub, which has one non-problematic branch snipped:
sub parsegooglealertbody { my ($html,$results) = @_; my $tree = HTML::TreeBuilder->new_from_content($html); my @paragraphs = $tree->look_down("_tag","p"); my $type; for my $i (0..$#paragraphs) { my $p = $paragraphs[$i]; my $ptext = $p->as_text; if ( $ptext =~ /blogs alert/i) { $type = 'blog'; next; } elsif ($ptext =~ /news alert/i) { $type = 'news'; next; } elsif ($ptext =~ /create another alert/i) { $type = ''; next; } my ($link,$title,$source,$sourceurl,$excerpt); if ($type eq 'news') { [snip] } elsif ($type eq 'blog') { my @anchors = $p->look_down("_tag","a"); if (@anchors) { nstore \@anchors, "anchors.sto"; $link = $anchors[0]->{href}; $link =~ s/\s+$//; ($title = $anchors[0]->as_text()) =~ s/<.+?>//g ; $title =~ s/^\s+|\s+$//g; $sourceurl = $anchors[1]->{href}; (my $temp = $anchors[1]->as_text()) =~ s/<.+?>//g ; ($source) = split(/ \- /,$temp); $source =~ s/^\s+|\s+$//g; my $snippet = $p->as_HTML; $snippet =~ s/<br ?\/?>/<br>/ig; my @segments = split(/<br>/i,$snippet); unless ($segments[1] =~ /color\=\"\#666666\"/i) { # ca +se with no byline ($excerpt = $segments[1]) =~ s/<.+?>//g; } else { # case with byline ($excerpt = $segments[2]) =~ s/<.+?>//g; } push(@{ $results->{blog} },{link => $link, title => $t +itle, source=> $source, sourceurl => $sourceurl, excerpt => $excerpt} +); } } } return $results; }
When it hits the line including $title = $anchors[0]->as_text() I get an error: Can't call method "as_text" on unblessed reference. However, as you can see $p is part of an HTML::Element object that is created in this sub, so it should be clear what package it's a part of.

As you can also see I dumped out @anchors into a Storable object so I could inspect it with a desktop debugger. When I retrieve this, it is an HTML::Element object, again it is created in an unambiguous context, i.e.

use Storable qw( retrieve ); use HTML::Element; use Data::Dumper; $anchors = retrieve('anchors.sto');
and it has content that should be accessible with the as_text method. But when I tried to dump it with Data::Dumper (for inclusion in this post), I also got the unblessed reference error.

All this is odd because I have used HTML::TreeBuilder in just this way in a lot of different scripts, and have never had this problem before. Anyone know what the problem is?

Thanks...

Steve