Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Unblessed reference in HTML::TreeBuilder and Data::Dumper

by cormanaz (Chaplain)
on Dec 10, 2011 at 17:11 UTC ( #942838=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Unblessed reference in HTML::TreeBuilder and Data::Dumper
Select or Download Code
Re: Unblessed reference in HTML::TreeBuilder and Data::Dumper
by Khen1950fx (Canon) on Dec 10, 2011 at 20:58 UTC
    I cleaned it up and tried a hashref instead of an arrayref; also, I shortened the sub name a wee bit. Try this:
    #!/usr/bin/perl -w use HTML::TreeBuilder; use Storable qw(nstore retrieve); use Data::Dumper::HTML qw(dumper_html); sub google_alert { use strict 'refs'; my ( $html, $results ) = @_; my $tree = 'HTML::TreeBuilder'->new_from_content($html); my (@paragraphs) = $tree->look_down( '_tag', 'p' ); my $type; foreach 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' ) { return; } elsif ( $type eq 'blog' ) { my (%anchors) = $p->look_down( '_tag', 'a' ); if (%anchors) { nstore( \%anchors, 'anchors.sto' ); $link = $anchors{'href'}; $link =~ s/\s+$//; ( $title = $anchors{'href'}->as_text ) =~ s/<.+?>//g; $title =~ s/^\s+|\s+$//g; $sourceurl = $anchors{'href'}; ( my $temp = $anchors{'href'}->as_text ) =~ s/<.+?>//g +; ($source) = split( / \- /, $temp, 2 ); $source =~ s/^\s+|\s+$//g; my $snippet = $p->as_HTML; $snippet =~ s[<br ?/?>][<br>]gi; my(@segments) = split( /<br>/i, $snippet, 0 ); if ( not $segments[1] =~ /color="\#666666"/i ) { ( $excerpt = $segments[1] ) =~ s/<.+?>//g; } else { ( $excerpt = $segments[2] ) =~ s/<.+?>//g; } push @{ $$results{'blog'}; }, { 'link', $link, 'title', $title, 'source', $source, 'sourceurl', $sourceurl, 'excerpt', $excerpt }; } } } return $results; }
Re: Unblessed reference in HTML::TreeBuilder and Data::Dumper
by Anonymous Monk on Dec 11, 2011 at 07:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942838]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-12-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls