Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Accessing Methods of Another Package

by mt2k (Hermit)
on Nov 29, 2002 at 20:29 UTC ( #216586=perlquestion: print w/replies, xml ) Need Help??

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

Another problem I have encountered whilst creating my set of modules. Say I have my set of modules that are named as follows:


Now, let's say that I have the following code in

#!/usr/bin/perl -w use strict; use Module::Main; my $REQ = new Module::Main; $REQ->init(); $REQ->start_it();
This is the start of it. Now, a couple of other modules are added to the mix...

Module::Main contains something like the following:

package Module::Main; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } sub init { my $self = shift; $self->{SEC1} = new Module::Section1; $self->{SEC2} = new Module::Section2; return 1; } sub start_it { my $self = shift; $self->{SEC1}->do_something(); return 1; }
And now, let's say that Module::Section1 contains something like this:

package Module::Section1; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } sub do_something { my $self = shift; =cut HERE! Now I need to access $REQ->{SEC2}->method(); The problem? $self now contains the object specific to Module::Section1 (ie: $REQ->{SEC1}, not to $REQ. So I can't do $self->{SEC2}->method(). I could always change the my() declaration in to an our() and then use $::REQ->{SEC2}->method(), but this just defeats the purpose of using OO programming! Besides, this would require that the variable be named $REQ and not anything else. I could always pass a reference to $REQ to everything subroutine/method, but that just seems extremely messy and bad programming style. So how can I accomplish this? =cut }

Replies are listed 'Best First'.
Re: Accessing Methods of Another Package
by chromatic (Archbishop) on Nov 29, 2002 at 22:00 UTC

    This is a design problem. If a Module::Section1 object conceptually can be said to contain a Module::Section2 object, then use composition. Otherwise, you need to decompose your objects again.

    Sorry to be so generic, but without your metaphor it's very difficult to be any more specific into the appropriate models.

Re: Accessing Methods of Another Package
by tedrek (Pilgrim) on Nov 29, 2002 at 20:46 UTC
    one way you could accomplish this is by keeping a reference to the parent in each of the section objects so your code becomes something like this
    package Module::Main ... sub init { my $self = shift; $self->{SEC1} = Module::Section1->new($self); $self->{SEC2} = Module::Section2->new($self); return 1; } package Module::Section1; sub new { my $class = shift; my $parent = shift || die; #require a parent.. my $self = {}; $self->{PARENT} = $parent; bless($self,$class); return $self; } sub do_something { my $self = shift; $self->{PARENT}->{SEC2}->method(); return; }
    update: Or you could keep a reference to the Section2 object which might be a better method OO wise.
Re: Accessing Methods of Another Package
by BUU (Prior) on Nov 30, 2002 at 06:13 UTC
    Uh, cheat a lot and do Module::Section2->method()?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-08-18 20:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found