Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Method "do" error

by b310 (Scribe)
on Apr 20, 2003 at 19:15 UTC ( [id://251856]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,
Happy Easter!
I'm trying to insert some data from a form into a table and I'm receiving the following error message:
Can't call method "do" on an undefined value
I'm having a tough time trying to figure this out.
Can someone lend me a hand?
Here's the code...
#! /usr/local/bin/perl -w use strict; use lib qw(/usr163/home/w/s/wstrader/public_html/library); use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard escape escapeHTML); use WebDB; my $dbh = WebDB::connect (); my $name = param("name"); my $coname = param("coname"); my $phone = param("phone"); my $email = param("email"); my $address = param("address"); my $city = param("city"); my $state = param("state"); my $zip = param("zip"); my $country = param("country"); my $page; my $choice = lc (param ("choice")); if ($choice eq "submit") # information submitted via form { store_items ($dbh); } else { $page .= p (escapeHTML ("Logic error, unknown choice: $choice")); } sub store_items { my $dbh; $dbh->do ("INSERT INTO contact_info (contact_date,contact_name,company,telephone,email,addre +ss,city,state,zip_code,country) VALUES (CURRENT_DATE,?,?,?,?,?,?,?,?,?)", undef, $name, $coname, $phone, $email, $address, $city, $state, $zip, $country); $dbh->disconnect(); }

Thank you for the help.

Replies are listed 'Best First'.
•Re: Method "do" error
by merlyn (Sage) on Apr 20, 2003 at 19:35 UTC
    You're declaring $dbh (which results in a new variable), and then immediately calling do on it! Of course it'd be undef. You'll need to do something to connect o the database before you use the database. Perhaps simply removing my $dbh from that subroutine will work, but I don't know what WebDB does.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hi,

      The WebDB is a module that connects to the database.
      I did verify that all the parameters in order to connect are correct. They are. The module works without a problem.

      All I'm trying to do is call in a few parameters from a form and insert them into a table.

        I think merlyn has already provided the answer. In the store_items subroutine, simply remove this line:

          my $dbh;

        From your description of WebDB, it seems likely that it is creating a DBI database handle object and storing it in a global variable called '$dbh'. By including 'my $dbh;' in the subroutine, you are creating a new variable with the same name that only exists within the subroutine and prevents access to the global. Because the variable in the subroutine is new, it is undefined.

        Since you have 'use strict' in effect, it is possible that removing the my $dbh; line will trigger the error: Global symbol "$dbh" requires explicit package name. If that is the case, you'll need to explicitly declare that it is a global variable like this:

          our $dbh;
Re: Method "do" error
by grep (Monsignor) on Apr 20, 2003 at 19:35 UTC
    Can't call method "do" on an undefined value
    This means exactly what it says, unfortunately.

    $dbh->do ("INSERT INTO contact_info...
    $dbh is undefined at that point

    If you add some error checking here I think you'll find your problem
    my $dbh = WebDB::connect ();

    grep
    Mynd you, mønk bites Kan be pretti nasti...

      Hi.

      The line my $dbh = WebDB::connect (); is not the problem. This is a module that allows me to connect to my database. I verified that the module is indeed connecting to the database. It is.

      Any other ideas?
Re: Method "do" error
by jsprat (Curate) on Apr 20, 2003 at 21:44 UTC
    Your code declares two lexical variables called $dbh, and each refers to a different variable. The second one masks the first - It's a brand new variable and it is undefined.

    In the sub, try my $dbh = shift; in place of my $dbh;.

      Hi.

      I tried your suggestion with my $dbh = shift; and I'm still having no luck.
        Where did you put the my $dbh = shift in the sub store_items or the main body of the code??

        it needs to be in the sub

        sub store_items { my $dbh = shift; ....

        If this is the case then follow my previous suggestion and use Data::Dumper to print out the database handle right before your error and see what you get.

        sub store_items { my $dbh = shift; use Data::Dumper; print Dumper $dbh; ...


        grep
        Mynd you, mønk bites Kan be pretti nasti...

Log In?
Username:
Password:

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

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

    No recent polls found