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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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=
          {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"; }

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?

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

On Fri, 01 Aug 2008 21:25:23 -0700, marko wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

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


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

Quoted text here. Click to load it

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.

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


Quoted text here. Click to load it

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".

Quoted text here. Click to load it

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


(remove .invalid and reverse each component for email address)

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

Site Timeline