Re: Object oriented mess
by JediWizard (Deacon) on Apr 13, 2005 at 14:22 UTC
|
Are you sure you understand the difference between use and require? Do you have a reason that your are requiring .pl files (which should be scripts, not modules)? I would recomend reading require and use. I would also reconsider the design of abc.pl if it needs to be included by other scripts.
A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you His Holiness, The Dalai Lama
| [reply] [Watch: Dir/Any] [d/l] |
Re: Object oriented mess
by VSarkiss (Monsignor) on Apr 13, 2005 at 14:26 UTC
|
Well, simply put, if adding use Client; breaks things, then the problem probably lies there. However, without knowing what "failure" means in this context (Do you get an error message? Does it work, but not return what you expect?) it's hard to say.
The first thing I suggest you do is read brian's Guide to Solving Any Perl Problem and follow its steps. I bet you find your answer around step 2 or 3.
| [reply] [Watch: Dir/Any] [d/l] |
|
You are right ... its something to do in Client.pm..
but thats such a simple module.
Here is the exact code. Not sure if allowed to post so many lines..
-----------------CLIENT.PM-----------------
#!/usr/bin/perl
package Client;
use strict;
# Include Database connect file here
require '/home/www/www/cgi-bin/db_connect.pl';
require '/home/www/www/cgi-bin/global_subs.pl';
sub new {
my($Class) = shift;
my($ClientId) = shift;
my $sth = $main::dbh->prepare ("SELECT client_fname,
client_lname,
email,
password,
status
FROM client_info
WHERE client_id = '$ClientId'
");
$sth->execute();# or &PrintMessage("$BaseDir/templates", $ErrorGen
+eralTmplt, "An Error has occurred.<p><A HREF=\"#\" onclick=\"history.
+back()\">click here</A> to go back.</P>", "exit");
my $hashref = $sth->fetchrow_hashref ();
my $Self = [
{
"FirstName" => $hashref->{'client_fname'},
"LastName" => $hashref->{'client_lname'},
"PrimaryEmail" => $hashref->{'email'},
"Password" => $hashref->{'password'},
"Status" => $hashref->{'status'},
}
];
bless $Self, $Class;
}
sub get_status {
my($Self) = shift;
return $Self->{"Status"};
}
1;
-------------------------------------------
| [reply] [Watch: Dir/Any] [d/l] |
|
A few notes (including an answer to your problem):
- Read up on placeholders in DBI. They will improve your database code, including fixing a few random bugs you may not have hit yet.
- Add warnings (either with the 'use warnings;' pragma if your Perl is 5.6+ or the -w flag if not). It will find bugs for you.
- You have a bug in your object. You are blessing an array reference (containing a single hash reference), but you are treating $self as a hash reference. This is a problem.
- You will want to look at 'use lib' to set up a way of shortening those require statements.
- Try varying the order you do the requires and the uses in your main script. Put the use before the requires then put the requires before the use. See if anything changes.
- Rewrite your requires to be modules using Exporter. Then, you use them and it will do the right thing, even if you use them in modules you're using (which is the problem here).
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
|
Re: Object oriented mess
by rev_1318 (Chaplain) on Apr 13, 2005 at 14:26 UTC
|
Your question has nothing to do with OOP.
You should read up on the use of use and require as well as on the use of subroutines (perlsub) before starting with OOP.
Paul | [reply] [Watch: Dir/Any] |
Re: Object oriented mess
by Joost (Canon) on Apr 13, 2005 at 14:28 UTC
|
Do you have strict and warnings enabled in all the files? You're probably overriding sub_in_abc() somewhere, and those pragma's should help you catch that sort of thing.
By the way, how do you know that sub_in_abc() "works fine when compiled individually"?
| [reply] [Watch: Dir/Any] |
|
If i simply not use
use Client;
the entire application works fine.
So I feel that there is something wrong with Client.pm. But I have been reading tutorials and sample scripts and moreove Client.pm just has two functions in it.
I spent several hours yesterday trying to figure out what i am missing here.
| [reply] [Watch: Dir/Any] |
|
Correct ... check out require.
Note that the file will not be included twice
under the same specified name.
Add into that mix that require happens at run-time, use
at compile and the notion that require pulls in the current namespace and voila - your subs are in your client namespace. (replace &sub_x with &Client::sub_x to see).
And that's why I try not to mix require with use. If I have
global functions -- then either create a base class and let your Client class inherit them or create a util class and let your Client class use them.
| [reply] [Watch: Dir/Any] |
|
So what is the error you're getting, and what is in Client.pm? My ESP is a little rusty today :-)
By the way, you're probably not overriding that subroutine because the use statement is run before the require()s.
I'd be surprised if use strict; and use warnings; wouldn't catch the error, though.
| [reply] [Watch: Dir/Any] |
|
|
Re: Object oriented mess
by RazorbladeBidet (Friar) on Apr 13, 2005 at 14:22 UTC
|
| [reply] [Watch: Dir/Any] |
|
Yes .. I do understand the difference between use and require.
abc.pl is a global file thats contains common sub-routines (such as Error handling subs, template formating sub, etc)
abc.pl is included in all files (even Client.pm)
Client.pm looks something like this
--------------client.pm-------------
package Client;
use strict;
require '../somepath/../abc.pl';
sub new {
create and return a hash object
}
sub get_name {
return $_[0]->{'Name'};
}
1;
-------------
I apologize if I am not explaining the code well enough. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: Object oriented mess
by monkey_boy (Priest) on Apr 13, 2005 at 14:49 UTC
|
My money is on faliure to find Client.pm in you're @INC.
Try:
use lib "/path/to/dir/your/module/in/"
use Client;
This is not a Signature...
| [reply] [Watch: Dir/Any] [d/l] |
|
Hey guys,
I have got some good pieces of advice here which is also leading to some kind of confusion.
The problem resolved a little when I commented out the
require ... require
lines in Client.pm
I am going to first heck out the Exporter module.
But could someone tell me what is the best way to include a global(common file which has carries commonly used subroutines) file in all my packages in perl files.
It seems that by using require and use together, is causing this error.
Thanks
| [reply] [Watch: Dir/Any] |
|
You shouldn't want to include a file.
It's a thinking in perl thing. In php, and some other languages, you include. In perl you "use." It's slightly more complicated than just inlining the file, but has a lot of benefits. I'll try and track down a reference about why you shouldn't want to "include" in perl.
Okay the reference: How do I include a file?
| [reply] [Watch: Dir/Any] |