0

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.

daliaessam
  • 1,636
  • 2
  • 21
  • 43
  • If you are using Moose, you do not need to write a constructor (`sub new`) yourself. That's what Moose is for. – simbabque Apr 17 '14 at 11:40
  • Also there are typos in the code. Please copy your exact code into the question and make sure it runs beforehand. Have you actually tried this? – simbabque Apr 17 '14 at 11:41
  • Not only do you not *need* to write `sub new` in Moose code, doing so is broken and wrong. – hobbs Apr 17 '14 at 15:41

1 Answers1

3

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • Based on your investigation, I did some search on auto loading modules and I found something like "use autouse 'Carp' => qw(carp croak);" may solve what you ara afraid of. Will that solve this issue and has no problems? if so why not we use "autouse" always to delay loading modules until it is really needed or used? – daliaessam Apr 18 '14 at 18:16
  • I found the module [later](http://search.cpan.org/~erwan/later-0.04/lib/later.pm) on cpan and used it very easy like `use later 'MyApp::Config';` the only drawback is, it injects sub AUTOLOAD into the package and the caller. – daliaessam Apr 20 '14 at 17:25