Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Can't call method "prepare" on an undefined value

by Massyn (Hermit)
on Dec 17, 2002 at 07:27 UTC ( #220467=perlquestion: print w/ replies, xml ) Need Help??
Massyn has asked for the wisdom of the Perl Monks concerning the following question:

#!/fellow/monks.pl

What does this mean? I'm using DBI and DBD-mysqlPP.

Can't call method "prepare" on an undefined value at mwslib.pl line xx +x.

The script works fine on my Redhat 6.2 box, but as soon as I upload my script to my ISP, I get this error in the error_log and the file fails to execute.

Any ideas on why this happens? Here's my procedure..

sub mwsexecutesql { local $mwssql = $_[0]; local $spl = $_[1]; local $field; local $data; local @ary; $data = ""; $mwssth = $mwsdbh->prepare(qq{$mwssql}) || &mwserror("DB Error +: \"" . $mwsdbh->errstr . "\" while preparing SQL statement \"$mwssql +\"",""); $mwssth->execute() || &mwserror("DB Error: " . $mwsdbh->errst +r . "\" while preparing SQL statement \"$mwssql\"",""); while (@ary = $mwssth->fetchrow_array()) { foreach $field (@ary) { chomp($field); $data = "$data$field$spl"; } $data = "$data\n"; } $mwssth->finish(); return substr($data,0,length($data)-1); }

Thanks

#!/massyn.pl The more I learn, the more I realize I don't know. Albert Einstein 1879-1955

Comment on Can't call method "prepare" on an undefined value
Select or Download Code
Re: Can't call method "prepare" on an undefined value
by MarkM (Curate) on Dec 17, 2002 at 07:34 UTC

    As the message says:

    Can't call method "prepare" on an undefined value at mwslib.pl line xxx.

    $mwsdbh must not be visible from within the scope of your subroutine, or not initialized by the time your subroutine is invoked. Another option is that $mwsdbh is not the correct spelling.

    Good luck...

Re: Can't call method "prepare" on an undefined value
by Zaxo (Archbishop) on Dec 17, 2002 at 07:39 UTC

    The undefined value is likely to be $mwsdbh. Did you recode your connection string and user data to reflect the new server? Your code excerpt doesn't show that essential part. In fact, how does your sub know what $mwsdbh is? How is it scoped? Do you have a closure?

    Always check for success when you go to the system.

    After Compline,
    Zaxo

Re: Can't call method "prepare" on an undefined value
by tachyon (Chancellor) on Dec 17, 2002 at 08:13 UTC

    $mwsdbh is not itintialized. As the problem occurs on upload I assume you have code like:

    my $mwsdbh = DBI->connect(blah) or do_not_warn_me_that_the_connect_fai +led_and_thus_mwsdbh_is_not_initialized()

    If the code works locally then your DBI connect is failing and you get no useful logging because you are not checking the return value....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Can't call method "prepare" on an undefined value
by graff (Chancellor) on Dec 17, 2002 at 10:08 UTC
    The script works fine on my Redhat 6.2 box, but as soon as I upload my script to my ISP, I get this error

    So, you're using mysql on your own redhat box, with your own set of tables, and you have a particular way of connecting to that database in your perl script.

    Are you sure that mysql is running on your ISP host? If so, do you know for sure that you have the same database setup there as you have on your local box? (Same connection parameters, same tables/schema?)

    (update: or am I clueless about something here... Is it the case that your script, once it's uploaded to the ISP host, is supposed to be connecting back to your own mysql server on your redhat box? If so, as tachyon indicated, it's just a matter of debugging the "connect()" call. It's likely that you need to alter one or more of the parameters in that call to make the connection work from the ISP host.)

Re: Can't call method "prepare" on an undefined value
by CountZero (Bishop) on Dec 17, 2002 at 12:48 UTC

    On an entirely different tack: are you sure you need to declare your variables as "local"?
    At first glance, "my" seems more appropriate.

    Also your die statement following the execute statement should really say "while executing SQL statement".

    Of course this does not help you with your problem at all.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I've played around with "strict" and "my", and funny enough, it doesn't fix the problem. I have found something else now... If I do not use procedures, ie. I do my mySQL connection, executing the SQL statement and disconnecting it all in one piece of code, it works fine, however, I'm using procedures to do that. I have a connection procedure, a disconnection procedure, and a SQL execution procedure. It looks like my $mwsdbh variable I receive from my connection is not a global variable, although it should be. When it comes to the execution of SQL statement procedure, the script dies.

      Have you guys seen this behaviour before? I've tried "returning" the variable, but that doesn't work.

      Thanks!

      #!/massyn.pl The more I learn, the more I realize I don't know. Albert Einstein 1879-1955

        Did you try sending the $mwsdbh variable as a parameter to the subroutines?
        That way it should be known and accessible in the subroutines.

        I'm still confused about the use you make of the "local" keyword for the declaration of your variables inside the subroutine. If you have a moment to spare, could you explain it?

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Can't call method "prepare" on an undefined value
by runrig (Abbot) on Dec 19, 2002 at 01:12 UTC
    As others have pointed out, $mwsdbh is undefined at that point in your subroutine. What does your connect look like? Are you checking for errors there? Easiest thing would be to set RaiseError in the connect (see the DBI docs). If $mwsdbh is set at some point in your code, then we'd need to know the context of where it is set and where this sub is (are they in different packages?).

    I would also take the advice to 'use strict', and start using my instead of local (unless you're still using perl 4, and then I'd say upgrade if at all possible).

      Here's my connect and disconnect procedures.. the %MWSPARAM hash gets populated with the necessary database information.

      sub mwsconnect { use DBI; local $server = $MWSPARAM{server}; local $db = $MWSPARAM{db}; local $user = $MWSPARAM{user}; local $passwd = $MWSPARAM{passwd}; #========================================== #make the connection #========================================== $mwsdbh = DBI->connect("DBI:mysqlPP:database=$db;host=$server" +, $user, $passwd) or &mwserror("DB Error: " . $DBI::errstr); } sub mwsdisconnect { $mwsdbh->disconnect(); }

      ...

      Thanks!

      #!/massyn.pl The more I learn, the more I realize I don't know. Albert Einstein 1879-1955

        #!/fellow/monks.pl

        I believe I found the problem... It's a bug in perl...

        Thanks!

        #!/massyn.pl The more I learn, the more I realize I don't know. Albert Einstein 1879-1955

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-08-28 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (263 votes), past polls