Error:Can't locate object method "prepare" via package "abc" at xyz.pm

Hi ,

Urgent help.

I have written 2 modules one abc.pm & xyz.pm (admin modules).

abc.pm
--------------------------------------
my $databasehandle;

sub new($){

    my ($self,$usr,$pwd) = @_;
    $usr||= "test";
    $pwd ||= "test123";
    ($self) = {};
    bless($self);
    $databasehandle = DBI->connect("DBI:Pg:dbname=mydb",$usr,$pwd,
{PrintError =>1});

        if (!$databasehandle){

                print "Database connection is not estabilished";
                exit;
                             }
        return($self);
        }


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

sub disconnect(){
        $databasehandle->disconnect() if ($databasehandle);
}




xyz.pm
----------

package admin;
use DBI;
use Apache::DBI;
use CGI;
use abc;
use strict;


my $database_handle =();

sub new()
{
my $self={};
bless($self);
$database_handle = abc->new('test','test123');
if (!$database_handle){

print "connection cant done:";
return -1;

}
print " connection done:";
return $self;

}

sub display()
{
my $self = shift;
my $sth = $database_handle->prepare("select * from table where status
='P'");
print $sth;
my $res =$sth->execute();
my @row = $sth->fetchrow_array();
print @row;
return $res;
}


I am getting an error :
Can't locate object method "prepare" via package "abc" at xyz.pm

Is it not possible the way i am trying...can anyone tell me where i am
wrong.

Thanks ,
Alma

0
almatirkey
5/31/2007 1:17:50 PM
perl.beginners 29388 articles. 4 followers. Follow

9 Replies
972 Views

Similar Articles

[PageSpeed] 20
Get it on Google Play
Get it on Apple App Store

Alma wrote:
> Hi ,
>
> Urgent help.
>
> I have written 2 modules one abc.pm & xyz.pm (admin modules).
>
> abc.pm
> --------------------------------------
> my $databasehandle;

Note that this establishes a single $databasehandle for every object
of type 'abc' that you create; it does not create a separate one for
each object.

> sub new($){
>
>     my ($self,$usr,$pwd) = @_;

Again, you have a signature problem.  'sub new($)' says that 'new'
will take a single scalar as a parameter; as such, @_ will only ever
have one value in it: $usr and $pwd will always be set to null.

Also, read up on the syntax of 'bless' a bit more.  IIRC, saying
'bless $self;' is not enough.

>     $usr||= "test";
>     $pwd ||= "test123";

....and thus $usr and $pwd will always equal "test" and "test123",
respectively.



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

You never set $self to anything.  Change the '()' in 'sub DESTROY()'
to '($)', or remove them altogether; then change 'my $self;' to 'my
$self = shift;' or 'my ($self) = @_;'

-- 
Jonathan "Dataweaver" Lang
0
dataweaver
5/31/2007 11:18:28 PM
On 05/31/2007 08:17 AM, Alma wrote:
> [...]
> $database_handle = abc->new('test','test123');
> [...]

No, the 'new' method of 'abc' returns an object of type 'abc'--not a 
database handle. Review the return statement in abc::new again.


> 
> sub display()
> {
> my $self = shift;
> my $sth = $database_handle->prepare("select * from table where status

This fails because $database_handle is not a database handle. You can 
force it to work by defining a 'prepare' method in the 'abc' package.

> [...]
> I am getting an error :
> Can't locate object method "prepare" via package "abc" at xyz.pm
> 
> Is it not possible the way i am trying...can anyone tell me where i am
> wrong.
> 
> Thanks ,
> Alma
> 
> 



0
mumia
6/1/2007 12:10:42 AM
On 5/31/07, Jonathan Lang <dataweaver@gmail.com> wrote:
snip
> Again, you have a signature problem.  'sub new($)' says that 'new'
> will take a single scalar as a parameter; as such, @_ will only ever
> have one value in it: $usr and $pwd will always be set to null.
snip

Well, there is a prototype problem, but it isn't that $ will force new
to only accept one value, but rather that prototypes and OO Perl don't
mix.  Perl simply ignores prototypes on methods.  Also prototypes are
broken*, don't use them.

#!/usr/bin/perl

use strict;
use warnings;

package foo;

sub new ($) {
        my $class = shift;
        return bless { @_ }, $class;
}

package main;

my $foo = foo->new(this => 1, that => 2);

print "this $foo->{this} and that $foo->{that}\n";

* http://library.n0i.net/programming/perl/articles/fm_prototypes/
0
chas
6/1/2007 12:54:24 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.
0
chas
6/1/2007 1:05:59 AM
Chas Owens wrote:
> Well, there is a prototype problem, but it isn't that $ will force new
> to only accept one value, but rather that prototypes and OO Perl don't
> mix.  Perl simply ignores prototypes on methods.  Also prototypes are
> broken*, don't use them.
-snip-
> * http://library.n0i.net/programming/perl/articles/fm_prototypes/

"Broken" and "don't use them" is a bit extreme.  But I will agree with
the general sentiment that they should not be used as a matter of
course; they should be reserved for a handful of special cases where
they help more than they harm.

-- 
Jonathan "Dataweaver" Lang
0
dataweaver
6/1/2007 1:34:01 AM
On 5/31/07, Jonathan Lang <dataweaver@gmail.com> wrote:
snip
> Also prototypes are broken*, don't use them.
> -snip-
> > * http://library.n0i.net/programming/perl/articles/fm_prototypes/
>
> "Broken" and "don't use them" is a bit extreme.  But I will agree with
> the general sentiment that they should not be used as a matter of
> course; they should be reserved for a handful of special cases where
> they help more than they harm.
snip

Yes, it is extreme, and, yes, there are a few occasions where they are
useful; however, you will never need them and dealing with
0
chas
6/1/2007 1:46:17 AM
On May 31, 7:18 pm, datawea...@gmail.com (Jonathan Lang) wrote:
> > abc.pm
> > --------------------------------------
> > my $databasehandle;
>
> Note that this establishes a single $databasehandle for every object
> of type 'abc' that you create; it does not create a separate one for
> each object.
>
> > sub new($){
>
> >     my ($self,$usr,$pwd) = @_;
>
> Again, you have a signature problem.  'sub new($)' says that 'new'
> will take a single scalar as a parameter; as such, @_ will only ever
> have one value in it: $usr and $pwd will always be set to null.

False.  Prototypes are *always* ignored on method calls, and can be
ignored on any other subroutine call by providing the & before the
subroutine name.  Therefore, there is no way of knowing from the above
what will be in $usr or $pwd.

> Also, read up on the syntax of 'bless' a bit more.  IIRC, saying
> 'bless $self;' is not enough.

You should *really* follow your own advice.  While not preferred, a
single arg bless() is perfectly legitamite.
$ perldoc -f bless
     bless REF,CLASSNAME
     bless REF
             This function tells the thingy referenced by REF
             that it is now an object in the CLASSNAME package.
             If CLASSNAME is omitted, the current package is
             used.

> >     $usr||= "test";
> >     $pwd ||= "test123";
>
> ...and thus $usr and $pwd will always equal "test" and "test123",
> respectively.

No, they will be set to "test" and "test123", respectively, if and
only if they had a false value prior to this step.  As discussed,
there is no way of knowing that from the above code.

Paul Lalli



0
mritty
6/1/2007 1:16:08 PM
On May 31, 2:17 pm, almatir...@gmail.com (Alma) wrote:
> Hi ,
>
> Urgent help.

[ snip - substantially the same thing as he posted a few days back ]

Please, if there is something you find unclear about the answers you
are given in a newsgroup or mailing list then follow-up with further
questions about the stuff you are finding unclear.

If you simply start a new thread asking the same questions again (as
you did) then you are likely to get substantially the same answers
again (as you have). You also run the risk of being perceived as lazy,
rude and arrogant.

> can anyone tell me where i am wrong.

Dunno. I tried the first time. You seem to have come back with the all
same problems so I get the impression you can't be told.

0
nobull67
6/1/2007 5:36:51 PM
On 06/01/2007 12:36 PM, nobull67@gmail.com wrote:
> On May 31, 2:17 pm, almatir...@gmail.com (Alma) wrote:
>> Hi ,
>>
>> Urgent help.
> 
> [ snip - substantially the same thing as he posted a few days back ]
> 
> Please, if there is something you find unclear about the answers you
> are given in a newsgroup or mailing list then follow-up with further
> questions about the stuff you are finding unclear.
> [...]

Huh? I can't find any such message from an "Alma" in the last few months 
on this mailing list.


0
mumia
6/1/2007 10:12:40 PM
Reply:

Similar Artilces:

Can't locate object method "prepare" via package "dbh"
--------------0A8D0A4ACF1C1C1361325891 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, I recently tested and installed the DBI and DBD:Oracle modules.I am a beginner to using DBI to connect to the Database. My first task is to connect to an Oracle Database using a 7.3.4 Oracle Client, with the SQL statement as "SELECT * FROM TAB" My program is as follows: #!/usr/local/bin/perl use DBI; my $dbuser = $ENV{ORACLE_USERID} || 's1_gwy/s1_gwy01'; my $dbh = DBI->connect('DBI:Oracle:', $dbuser, '') ...

[Software Error] Can't locate object method "new" via package "Bugzilla::Template" at Bugzilla/Template.pm line 977
Hi, Yesterday I downloaded latest stable version (4.2.1) of bugzilla and I resolved all dependencies even checksetup.pl also run successfully I also checked testagent.cgi which also show OK. but when I use index.cgi, It is showing error which is in subject line. I searched in google and found one suggestion "may be code corrupted" but I downloaded again and did md5sum on file, every thing looks as expected. Can someone please tell me why that behaviour I am getting. Thanks and Regards, Praveen Kumar http://fedoraproject.org/wiki/User:Kumarpraveen http://fedoraproje...

"Can't locate object method "epath" via package "URI::http""
Greetings, I've been trying to debug why I can't issue a form -> click and finally started debugging it as a standalone script (as opposed to viewing CGI output in a browser, duh) I get the following error msg when I issue the form->click ..... Can't locate object method "epath" via package "URI::http" at /usr/local/lib/perl5/site_perl/5.005/HTTP/Cookies.pm I'm using the same User Agent that I defined earlier in my script that was used to pull several pages (including the form I'm trying to submit) using cookies. My latest scrip...

Software Error: Can't locate object method "now" via package "DateTime"
I am getting following error when I am trying to access "Whining" page, during bugzilla administration. Can't locate object method "now" via package "DateTime" at .../bugzilla/editwhines.cgi line 386. Please help me out. Thanks, Farhan On Wednesday, June 5, 2013 10:26:24 PM UTC-7, farhan...@gmail.com wrote: > I am getting following error when I am trying to access "Whining" page, during bugzilla administration. > > Can't locate object method "now" via package "DateTime" at .../bugzilla/editwhines.cgi l...

Can't locate object method "fixin" via package "MY"
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I have a Makefile.PL which works with perl 5.6.0 and 5.6.1, but the new MakeMaker 5.49_01 on perl 5.6.1 breaks it. Here is a minimal test case: use ExtUtils::MakeMaker; WriteMakefile(EXE_FILES => [ 'my_exe' ]); Under perl 5.6.0 (and also 5.6.1, I'm pretty certain) this works: % perl Makefile.PL Warning: Guessing NAME [packaged] from current directory name. Writing Makefile for packaged % make cp my_exe blib/script/my_exe /usr/bin/perl -I/usr/lib/perl5/5.6.0/i586-linux -I/usr/lib/perl5/5.6.0 \ - -MExtUtil...

Can't locate object method "new" via package "MD5"
I've managed to get around my initial problems of a few days ago by installing an update of the dev tools, but am now facing a new problem. I'm trying to install MD5, here's the output: ===snip=== cpan> force install MD5 Running install for module MD5 Running make for G/GA/GAAS/MD5-2.02.tar.gz Scanning cache /var/root/.cpan/build for sizes CPAN: MD5 security checks disabled because MD5 not installed. Please consider installing the MD5 module. MD5-2.02 MD5-2.02/Makefile.PL MD5-2.02/MD5.pm MD5-2.02/Changes MD5-2.02/t MD5-2.02/t/md5.t MD5-2.02/README ...

Can't locate object method "connect" via package "DBI"
------=_NextPart_000_001B_01C102EB.C7598F30 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi I have upgraded my machine as following RHLinux 6,.2 --> 7.0 PostgreSql 65. --> 7.0 Perl 5 -->Perl 5.6 I have installed DBI & DBD again after the upgradation. My problem is my old perl codes (which were running perfectly before upgradation) uses connect are not running. It can't connect postgresql databse through DBI. The error is --- Can't locate objrect method "connect" via package "DBI&qu...

Can't not locate object method "isadmin" via package "Noc1"
Hello all, I just added a new method called isadmin to existing and working module Noc1.pm And use this new added method in my index.html like this use Noc1; my $noc = new noc1; my public = noc->ispublic(); my $admin = noc->isadmin(); if ($admin) { blah blah blah} When i tried to access index.html i got the error msg... can't locate object method "isadmin" via package 'Noc1" at ..../index.html line 21 Do you know what is the problem here ? Everything works fine with Noc1.pm and index.html until i add my new method isadmin in. thank...

Can't locate object method "trace_msg" via package "DBI"
Please help in order to fix the problem, I am newbie in the httpd.conf I have put the bellow lines: Alias /perl/ /usr/apache/perl5/5.005/i86pc-solaris/Apache/ PerlModule DBI When I try to run the httpd I get: Can't locate object method "trace_msg" via package "DBI" at /usr/perl5/site_perl/5.005/i86pc-solaris/DBI.pm line 341. END failed--cleanup aborted. run: Error 0 I am using the bellow software: OS: solaris 8/intel httpd: apache 1.3.20 mysql: 3.23.38-1 DBI: 1.18 (I have compiled using gcc) Thank you in advance Why do you put that in http...

Can't locate object method "AppendSubMenu" via package "Wx::Menu"
After uploading Padre 0.02 I got several reports with the following failure: Can't locate object method "AppendSubMenu" via package "Wx::Menu" There were reports both using both Wx 0.83 and 0.84 on various platforms. http://www.nntp.perl.org/group/perl.cpan.testers/2008/07/msg1948047.html http://www.nntp.perl.org/group/perl.cpan.testers/2008/07/msg1948052.html The same code seem to work on my computer. All the 8 failing reports are from the same smoker (Andreas Koenig) so there might be some issue with his computer or the fact that they are in the smoki...

Can't locate object method "host" via package "URI::_foreign"
Hello, I get this error: Can't locate object method "host" via package "URI::_foreign" at ../isaloganv11.pl line 82, <> line 31668 Line 82 of the said perl script: $line[18] = $uri->host; Line 31668 from the file being parsed: 10.6.66.167, anonymous, -, N, 8/2/2003, 16:15:11, w3proxy, ISASERVER, -, -, -, 0, 0, 0, 0, -, TCP, -, -, -, -, 0, 0x0, -, - Script location: http://www.geocities.com/losttoy2000/isaloganv10.pl My script crashes when it encounters this error!! :( Regards, Siddhartha Jain Certified Information Systems Security ...

Can't locate object method "new" via package "Bugzilla::CGI"
Hi Everyone, I'm completely new to Bugzilla , which I am trying to get-going on Win XP. I have installed MySQL 5.0.41 & Apache 2.2, Activestate Perl 5.8.8. Executing checksetup.pl shows all required perl modules as available (I also installed Bundle::Bugzilla through PPM), but when I try to browser to http://localhost/index.cgi, I get an HTTP500 & the following error in the Apache log: "Can't locate object method "new" via package "Bugzilla::CGI". Does anyone have any idea what might be wrong. Would really appreciate any help. Many ...

Can't locate object method "gif" via package "GD::Image"
--=-R4A85o2oPiKml+WC6GBo Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi all, I have installed Perl-GD , and tried to use the GD::Image it throws the following error message , Can't locate object method "gif" via package "GD::Image" how can i eliminate the above error? Thanks, --=-R4A85o2oPiKml+WC6GBo-- On Wed, 02 Apr 2008 17:37:11 +0530, msivasakthi@gmail.com (sivasakthi) wrote: >Hi all, > > >I have installed Perl-GD , and tried to use the GD::Image > >it throws the following error message , >...

Can't locate object method "host" via package "URI::_generic"
Hello, I'm trying to use Cookies with LWP and it does not seem to be working. I get the following error: Can't locate object method "host" via package "URI::_generic" at /usr/share/perl5/HTTP/Cookies.pm line 725. My cookie initialization looks like: $ua = LWP::UserAgent->new; $ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 " . $ua->agent); $ua->cookie_jar(HTTP::Cookies->new(file => "cookies.txt", autosave => 1)); And my request object is used as so: my $res = $ua->request(GET $url); I&#...

Web resources about - Error:Can't locate object method "prepare" via package "abc" at xyz.pm - perl.beginners

Resources last updated: 1/9/2016 12:00:14 PM