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

Discussion in 'Perl Misc' started by marko, Aug 2, 2008.

  1. marko

    marko Guest

    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?
     
    marko, Aug 2, 2008
    #1
    1. Advertising

  2. 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
     
    Martijn Lievaart, Aug 2, 2008
    #2
    1. Advertising

  3. marko

    marko Guest

    On Aug 2, 6:30 am, Martijn Lievaart <> wrote:
    > 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


    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.
     
    marko, Aug 2, 2008
    #3
  4. marko <> wrote in news:d5a3ad51-1e35-4e09-8441-
    :

    > 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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Aug 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulloa
    Replies:
    1
    Views:
    543
    Juha Laiho
    Jul 22, 2004
  2. Jerome Hauss
    Replies:
    0
    Views:
    184
    Jerome Hauss
    Oct 13, 2004
  3. bigjojoi
    Replies:
    0
    Views:
    96
    bigjojoi
    Jul 1, 2003
  4. Asby

    Mason, DBI, and DBI::Pg

    Asby, Jul 24, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    188
  5. Tim Haynes
    Replies:
    3
    Views:
    152
    Ron Reidy
    Sep 13, 2003
Loading...

Share This Page