Search code examples
perlmoose

Perl cyclic use modules


I need to know if this practice for using modules fine or not:

MyApp.pm

package MyApp;
use Moose;
use MyApp::View;
use MyApp::Config;

sub view {
    return MyApp::View->new;
}
sub config {
    return MyApp::Config->new;
}

MyApp/View.pm

package MyApp::View;
use Moose;
extends qw(MyApp);

sub render {
}

MyApp/Config.pm

package MyApp::Config;
use Moose;
extends qw(MyApp);

sub get {
}

App.cgi

#App.cgi
use Moose;
extends qw(MyApp);
my $view = MyApp->view();
my $config = MyApp->config();
....

I am confused since I used "use MyApp::View" in MyApp then used "extends qw(MyApp);" in Config module. Is that considered bad cyclic?.

The idea about this I want to share all methods and variable in the MyApp module with the View and Config modules in the same instance in App.cgi.


Solution

  • This is pretty normal and not particularly bad. The only caveat is that while compiling and running the package body for MyApp::View and MyApp::Config, MyApp won't be completely compiled, and some of its methods might not exist, because MyApp can't continue compiling until after MyApp::View and MyApp::Config load. During the normal execution of the app (after use MyApp completes), there's no such problem. Since you're not doing anything interesting in BEGIN blocks or in the package bodies themselves, I don't see any problem.