Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problem with HTML::Parser

by artyom (Initiate)
on Mar 11, 2012 at 20:51 UTC ( #959019=perlquestion: print w/ replies, xml ) Need Help??
artyom has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys! I have a problem. I get the error: can't call method "do" on an undefined value

#!/usr/bin/perl -w use strict; package SF; use base "HTML::Parser"; use DBI; my $f_header = 0; my $f_name = 0; my $f_description = 0; sub start { my ($self, $tag, $attr, $attrseq, $origtext) = @_ ; if ($tag eq 'header') { $f_header = 1 }; if ($tag eq 'span' && $attr->{'itemprop'} eq 'name') { $f_name = 1 + }; if ($tag eq 'a' and $f_header = 1) {my $href = $attr->{'href'} }; if ($tag eq 'p' && $attr->{'itemprop'} eq 'description') { $f_desc +ription = 1}; } sub text { my ($self, $text) = @_ ; my $name; my $href; if ($f_name = 1) {my $name = $text} if ($f_description = 1) { my $description = $text; ############## here the error ############# my $dbh->do("INSERT INTO names VALUES('$name', '$href', '$descri +ption')"); ############################################ } } sub end { my ($self, $tag, $origtext) = @_ ; if ($tag eq 'header') { $f_header = 0 } if ($tag eq 'span') { $f_name = 0 } if ($tag eq 'p') { $f_description = 0 } } package main; use strict; use DBI; use LWP::Simple; my $parcer = SF->new; my $dbh = DBI->connect("dbi:SQLite:dbname=/media/DOOMGL/perl/db.sql +t","",""); $dbh->do("DROP TABLE names"); $dbh->do("CREATE TABLE names (name, href, description)"); my $page; for ($page = 1, $page < 3, $page++) { my $url = "http://sourceforge.net/directory/os%3Alinux/freshnes +s%3Arecently-updated/?page=" . "$page"; my $content = get($url); $parcer -> parse($content); }

Comment on Problem with HTML::Parser
Download Code
Re: Problem with HTML::Parser
by tobyink (Abbot) on Mar 11, 2012 at 21:04 UTC

    Your my $dbh defines a variable called $dbh which is initially set to undef. And then you call a method ->do on it. But (unless you use autobox, which almost nobody does for production code as it's just too weird) you can't call methods on undef.

    I assume you actually want to use the $dbh variable you defined in main. Long term you want to read up about how scoping works in Perl. Short term a solution is to use $::dbh instead of $dbh, and don't use my in front of it. This acts as a global variable, so will be accessible from anywhere in your program. However, global variables are not a good idea, which is why long term you should learn how Perl scoping works.

Re: Problem with HTML::Parser
by repellent (Priest) on Mar 11, 2012 at 22:16 UTC
    Check for errors when you connect:
    my $dbfile = "/media/DOOMGL/perl/db.sqlt"; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "") or die $DBI::errstr;

    When you fail to connect, $dbh will be undef.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-07-02 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls