Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Object oriented mess

by longliveAJ (Acolyte)
on Apr 13, 2005 at 14:14 UTC ( [id://447396]=perlquestion: print w/replies, xml ) Need Help??

longliveAJ has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I just started using OOP and encountered some weird problem. Not sure what I am missing out. I have a perl file main.pl where my application starts. main.pl looks something like this
----------------main.pl--------------------- require '../somepath/../abc.pl'; require '../somepath/../def.pl'; # Call a sub-routing in abc.pl &sub_in_abc(); ---------------------------------------------

This works fine. I get the results. In other words I am able to call sub-routines in other files after including them. Now I wrote a module called Client.pm
This is simple module and has two functions in it
However when I include this module in main.pl I receive an error
----------------main.pl--------------------- require '../somepath/../abc.pl'; require '../somepath/../def.pl'; use Client; # Call a sub-routing in abc.pl &sub_in_abc(); ---------------------------------------------
The function call to subroutine in abc.pl now fails.
All the files are in the same folder and each works fine when compiled indiviually.
What am I missing here ?
Thanks

Replies are listed 'Best First'.
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

Re: Object oriented mess
by VSarkiss (Monsignor) on Apr 13, 2005 at 14:26 UTC
      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; -------------------------------------------
        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).
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

Re: Object oriented mess
by Joost (Canon) on Apr 13, 2005 at 14:28 UTC
      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.
        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.

        -derby
Re: Object oriented mess
by RazorbladeBidet (Friar) on Apr 13, 2005 at 14:22 UTC
    Can you provide more code about abc.pl?

    More information about Client? Are they related in any way?

    What error are you getting?
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      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.
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...
      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
        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://447396]
Approved by Corion
Front-paged by tlm
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found