Re: Error:Can't locate object method prepare via package abc at xyz.pm
On May 31, 7:18 pm, [EMAIL PROTECTED] (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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
On May 31, 2:17 pm, [EMAIL PROTECTED] (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. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
On 06/01/2007 12:36 PM, [EMAIL PROTECTED] wrote: On May 31, 2:17 pm, [EMAIL PROTECTED] (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. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
On 5/31/07, Jonathan Lang [EMAIL PROTECTED] 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/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
On 31 May 2007 06:17:50 -0700, Alma [EMAIL PROTECTED] 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. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Error:Can't locate object method prepare via package abc at xyz.pm
On 5/31/07, Jonathan Lang [EMAIL PROTECTED] 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/