Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by Khen1950fx (Canon)
on Dec 10, 2011 at 20:58 UTC ( [id://942869]=note: print w/replies, xml ) Need Help??


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

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://942869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found