<div class="gmail_quote">On Sat, Apr 30, 2011 at 11:29 AM, George Hartzell <span dir="ltr"><<a href="mailto:hartzell@alerce.com">hartzell@alerce.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Chris Weyl writes:<br>
 > Hm.  Do you actually need these modules loaded?  (I'm guessing not)  One way<br>
 > I've dealt with things like this using, e.g., MooseX::App::Cmd and having a<br>
 > role wrap execute() in each of the command classes, that then calls<br>
 > Class::MOP::load_class() against any modules the command needs to do its<br>
 > thing if the command is told to run.  (MX::App::Cmd is just what I tend to<br>
 > use; the same methodology would work elsewhere.)  This doesn't do any<br>
 > pre-compiling, but helps simply by cutting down to loading only what's<br>
 > needed.<br>
</div> > [...]<br>
<br>
Hi Chris,<br>
<br>
Can you post an example of this?  Or a bit more complete sketch?<br>
<br>
I have several a MooseX::App::Cmd based classes that could probably<br>
benefit from this...<br></blockquote></div><div><br></div><div>Sure -- imagine something like this:</div><div><br></div><div>package MyApp::CommandRole::Lazy;</div><div><br></div><div>use Moose::Role;</div><div>use namespace::autoclean;</div>

<div><br></div><div>requires '_lazy_classes';</div><div>requires 'execute';</div><div><br></div><div>before execute => sub { Class::MOP::load_class($_) for $_[0]->_lazy_classes };</div><div><br></div>

<div>...then over in a command class:</div><div><br></div><div>package MyApp::Command::something;</div><div><br></div><div>use Moose;</div><div>use namespace::autoclean;</div><div>extends 'MooseX::App::Cmd::Command';</div>

<div>with 'MyApp::CommandRole::Lazy';</div><div><br></div><div>sub _lazy_classes { qw/ Big::DBIC::Schema OtherLargeClass ... / }</div><div><br></div><div>sub execute { ... }</div><div><br></div><div>etc.  This way everything MX::App::Cmd needs for introspection loads properly every time the app/tool is run, but only when a given command class is actually execute()'ed do the big packages get loaded.</div>

<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div>The same method works with extending MooseX::App::Cmd::Command for a custom base class for your CLI tools.  The advantage to the role here is that you don't need to worry about making sure the lazy classes are loaded by anything execute() does in your command classes: including the role and providing the required methods is sufficient.</div>

<br>                                    -Chris<br>-- <br>Chris Weyl<br>Ex astris scientia<br>