Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
DBI:Pg script returning 'can't call method' error marko 08-02-2008
Get Chitika Premium
Posted by marko on August 2, 2008, 12:25 am
Please log in for more thread options
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?



Posted by Martijn Lievaart on August 2, 2008, 6:30 am
Please log in for more thread options
On Fri, 01 Aug 2008 21:25:23 -0700, marko wrote:

> 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

Posted by marko on August 2, 2008, 8:18 am
Please log in for more thread options
> On Fri, 01 Aug 2008 21:25:23 -0700, marko wrote:
> > if (undef $dbh) {die "cannot connect to database:$!\n";} else {print
> =A0 ^^^^^^^^^^^^^^^
> > "Success connecting!\n";} my $statement =3D "select count(*) from
>
> Which means: Undefine the $dbh variable; if (undefined value =3D=3D 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.


Posted by A. Sinan Unur on August 3, 2008, 8:37 am
Please log in for more thread options
ddac55d3cb05@l64g2000hse.googlegroups.com:

> 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

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

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

Similar ThreadsPosted
upload method not returning undef? March 21, 2007, 5:37 am
Can't call method "Sql"... need help February 28, 2007, 6:13 am
Cant' call method "EOF" on an undefined value... January 18, 2006, 5:41 pm
Ambiguous method call November 27, 2007, 7:29 am
ExpatXS: 'Can't call method "read" on an undefined value' after ca.500 XML files January 13, 2006, 4:24 pm
Can't call method "mail" - w3mail/cascade soft broken October 5, 2006, 12:26 pm
Event Can't call method GetValue by thicking dynamicly createt checkbox July 9, 2005, 12:29 pm
Script Not Returning Value January 15, 2005, 11:40 am
Getting error 500 when removing sub call April 26, 2006, 5:46 pm
HELP! "Method Missing" error message driving me insane. October 7, 2005, 6:21 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap