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

I can't wrap my head around this. I have a history of Perl experience, but not so much in writing modules. So here is what I am trying to do. I am building an MVC web app. I started with the M part. I am abstracting the Model class, and then having different storage classed bolt on. The structure looks like this: Model:
App::Model App::Model::User
Storage:
App::Model::Storage::DBI App::Model::Storage::FlatFile
Then I create an instance of the model in my app like this:
my $model = App::Model->new( storage => 'DBI', config => '...', );
Then I'd like to be able to use the model like this:
my $user = $model->user->find_by_email(...); # finds user by email and + returns App::Model::User object $user->password('123456'); # change password $user->save; # save user profile
So where I got stuck was sharing the config and storage information between the App::Model and App::Model::User class (and other children). What's the best approach? Are there any good modules to take a look at that implement something a similar way?

Replies are listed 'Best First'.
Re: Sharing data between children modules
by Joost (Canon) on Dec 09, 2007 at 19:16 UTC
    Assuming the config and storage info is global (at least as seen from any particular call in the system), you can stuff them anywhere you like, but "logically" it would go either somewhere in the top model package or in a separate configuration package.

    As for access, I'd recommend you use methods or regular subroutines that return the right data/object for the current context, since that way you can change the implementation without having to change all the code relying on the information.

    update: as a variant of this, if the relevant info is all provided by the top-level model object, you can also make the model object globally available from a class method/subroutine and then get the data from there.

Re: Sharing data between children modules
by ian (Beadle) on Dec 10, 2007 at 17:54 UTC
    I'm assuming you already know about Catalyst and CGI::Application. But I thought I'd mention these in case you haven't; they both utilize MVC.
    -- Ian Tegebo