Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Module/Sub-module Structure

Status
Not open for further replies.

Kirsle

Programmer
Joined
Jan 21, 2006
Messages
1,179
Location
US
I'm looking for some tips on how to organize a module system that would have sub-modules under it but still be treated as one object.

Here's a simple example of what I'm talking about:

Say I have a module system set up as follows:

Business::Customers - root module
Business::Customers::Modify - modification of customer data
Business::Customers::View - viewing of customer data

Now, the source code of the Perl script using these modules might look like this:

Code:
use Business::Customers;

my $object = new Business::Customers;

# load the data from a file... all
# customer data is kept in memory in
# the $object space.
$object->loadFromFile ("customers.dat");

# call Business::Customers::Modify to
# change the data for one customer
$object->modify->newVariable (
   for    => 'A. U. Thor',
   newVar => 'newValue',
);

Now, the source to Business::Customers would look like this:

Code:
package Business::Customers;

use Business::Customers::Modify;
use Business::Customers::View;

sub new {
   my $class = shift;

   # create the other objects
   my $self = {
      modify => new Business::Customers::Modify,
      view   => new Business::Customers::View,
   };
   bless ($self,$class);
   return $self;
}

# method to load data into memory...
sub load {
   ...
}

# these methods return the sub-objects so that
# $object->modify->... works
sub modify {
   return shift->{modify};
}
sub view {
   return shift->{view};
}

Now, the question:

How would a sub-module, say, Business::Customers::Modify, have access to the hashref object of Business::Customers to read and modify its variables?

I've programmed one module already, named RiveScript, which used sub-modules in this way. But the only way I could figure out for them to "share" variables was to copy $self into the new objects, and then copy keys back from the new object into the main object, finally to delete the new object to clear up memory again.

How can the sub-modules have full access to the data structure of the main object without having to clone and copy it?
 
Have you found the answer?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top