For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > Re: Error:Can't locate object method "prepare" via package "abc"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Error:Can't locate object method "prepare" via package "abc"
Chas Owens

2007-06-01, 7:58 am

On 31 May 2007 06:17:50 -0700, Alma <almatirkey@gmail.com> wrote:
snip
> I am getting an error :
> Can't locate object method "prepare" via package "abc" at xyz.pm

snip

Besides the things that are wrong that other people have mentioned,
you need to make the abc class a subclass of DBI. This being Perl
there are several ways to do it, but I prefer to say

package DBI::MySingleton;

use strict;
use Carp;
use base 'DBI';

our $dbh;

sub new {
our $dbh;
my ($class, $user, $pass) = @_;
$user ||= "test";
$pass ||= "test123";

unless ($dbh and $dbh->ping) {
$dbh = DBI->connect("DBI:Pg:dbname=mydb",$user,$pass, {PrintError =>1})
or croak DBI->errstr;
bless $dbh, $class;
}

return $dbh;
}

sub DESTROY() {
my $self = shift;
$self->disconnect();
}

1;


Or you could just use DBI->connect_cached() and save yourself a heap of trouble.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com