DBI:Pg script returning 'can't call method' error


M

marko

This is my script in its current (troubleshooting) state:
----------------------------------------------------------
#!/usr/bin/perl -w
use DBI;
#use DBD::pg;
use strict;
my $dbname = "test";
my $dbh = DBI->connect("DBI:pg:dbname=
$dbname;host=localhost;port=5432","","",
{RaiseError => 1, PrintError => 1 })
or die "Can't connect to the db: $DBI::errstr\n";
if (undef $dbh) {die "cannot connect to database:$!\n";}
else {print "Success connecting!\n";}
my $statement = "select count(*) from tex_births";
my $rc = $dbh->ping; # this is the line returning the 'can't call
method 'ping' error'
print $rc . "\n";
#my $sth = $dbh->prepare($statement)
# or die "Can't prepare SQL statement: $DBI::errstr\n";
#if ( undef $sth ) {die "Could not prepare statement:$!\n";}
#$sth->execute or die "Can't Execute statement: $DBI::errstr\n";
while (my @row_ary = $dbh->selectrow_array($statement)) {
#while ( my @row = $sth->fetchrow_array() ) {
print @row_ary,"\n"; }
#$sth->finish;
$dbh->disconnect;
-------------------------------------------------------------------------

Perl, this script and postgresql are all running on this same
machine. The connect() method is what I would suspect as being the
culprit, naturally, but the die is not being triggerred and a
$DBI::errstr is not being printed out. In fact, a 'Success
connecting!' is instead being printed and the next output line is
'can't call method 'ping' on an undefined value'.

The log file to postgresql doesn't show anything out of the ordinary
at all! The psql command line is working fine connected to the
postgresql server, so I don't think its a server issue. **I'm really
stumped on this one.** Would anyone like to take a stab at helping me
find my way? Please?
 
Ad

Advertisements

M

Martijn Lievaart

if (undef $dbh) {die "cannot connect to database:$!\n";} else {print ^^^^^^^^^^^^^^^
"Success connecting!\n";} my $statement = "select count(*) from

Which means: Undefine the $dbh variable; if (undefined value == true)
{ die no connect } else { print success };

You probably want:

die "cannot connect to database:$!\n" unless defined $dbh;
print "Success connecting!\n";

HTH,
M4
 
M

marko

Which means: Undefine the $dbh variable; if (undefined value == true)
{ die no connect } else { print success };

You probably want:

die "cannot connect to database:$!\n" unless defined $dbh;
print "Success connecting!\n";

HTH,
M4

Whoopsie! Thanks. Wow. I gotta say as much as I like perl, it can
really be very unintuitive at times and I think the vast difference
between defined and undef is one of them. undef will change the value
of your variable and defined is a test!

Well, that was obviously my problem. It works now. Thanks.
 
Ad

Advertisements

A

A. Sinan Unur

I think the vast difference between defined and undef is one
of them. undef will change the value of your variable and
defined is a test!

It is not as if the difference is a secret. If the passive voice used in
'defined' versus the active 'undef' is not enough of a clue, note:

perldoc -f undef

undef Undefines the value of EXPR,

perldoc -f defined

defined Returns a Boolean value telling whether EXPR has a value other
than the undefined value "undef".
Well, that was obviously my problem. It works now.

Good. Keep in mind that reading parts of the documentation is a useful
and enjoyable habit even when you do not have a problem.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top