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

OOP related, using methods in another object without inheriting them.

by Steeeeeve (Initiate)
on Feb 05, 2001 at 21:26 UTC ( #56442=perlquestion: print w/replies, xml ) Need Help??

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

Help! I'm trying to avoid being further deprecated in this forum. I created an object and within that object I'm trying to use methods of another object. The first object is created as:
package SITE::USERS_lib; # use strict; # $USERS_libPackage = "USERS_lib"; # $USERS_libPackage::Version = "001108"; # $::USERS_lib = $USERS_libPackage; use Lady::Lady_TM; use Lady::Lady_DATE; # sub new(%){ my $class = shift; my $self = {}; bless $self, $class; local (%SITE) = @_; $self{'access_log_path'} = $SITE{'access_log_path'}; %SITE=(); $self->init(); return $self; } sub init(){ my $ref = new Lady_TM("$self{'access_log_path'}"); return; }
With the second object created as:
package Lady::Lady_TM; #use strict; sub new ($){ my $class = shift; my $self = {}; bless $self, $class; $self->init($_[0]); return $self; } sub init($){ my $self = shift; $self{'index_path'} = shift; return; }
When I run code that calls the new method in the first object I get an error when I attempt to create a reference to access methods in the second object. At this line: my $ref = new Lady_TM("$self{'access_log_path'}"); I am getting this error: Undefined subroutine &SITE::USERS_lib::Lady_TM called at C:/InetPub/wwwroot/GREENTV2/library/SITE/USERS_lib.pm line 44. I've tried everything,, the error is new. I know it has something to do with the way I'm creating my objects. -Steeeeeve

Replies are listed 'Best First'.
Re: OOP related, using methods in another object without inheriting them.
by kilinrax (Deacon) on Feb 05, 2001 at 21:41 UTC
    The most obvious problem I can see is:
    my $ref = new Lady_TM("$self{'access_log_path'}");
    needs to be:
    my $ref = new Lady::Lady_TM("$self{'access_log_path'}");
      YES! Thank you so much. I made some changes in working code and it died on me. Its working again. I'm on the road to creating packages in the proper manner. That will of course be relected by changes in my posts. Thanks. -Steeeeeve
Re: OOP related, using methods in another object without inheriting them.
by runrig (Abbot) on Feb 05, 2001 at 21:34 UTC
    Note that this would be a very un/anti-OO way to go about using OO methods. But here's a possibility:
    my $obj1 = Package_one->new; my $obj2 = Package_two->new; my $results = $obj1->can('package_one_method')->($obj2, @more_args);
    The 'can' method will return a code reference to the first objects method, and then you can pass that method the second object, and more arguments if necessary.

    You can probably expect to be 'deprecated' for using methods in this way, though. But hey, at least perl lets you do it, whereas other 'OO' languages wouldn't :-)

    Update:Your second init method is using '$self' as if it were a hash, when it is a hash ref (it should be "$self->{'index_path'} = shift", not "$self{'index_path'} ..."). And the same thing goes for your first new method (should be "$self->{'access_log_path'} ...".

(tye)Re: OOP related, using methods in another object without inheriting them.
by tye (Sage) on Feb 05, 2001 at 21:43 UTC

    The other package is Lady::Lady_TM, not just Lady_TM. But I'd rewrite that even further as:

    Lady::Lady_TM->new($self{access_log_path})
    No comment on whether you should be doing this. (:

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-07-04 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?