http://qs321.pair.com?node_id=216586

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:

Module::Main
Module::Section1
Module::Section2

Now, let's say that I have the following code in index.pl:

#!/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 index.pl 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 }