[sf-perl] making perl compile phase faster

Chris Weyl cweyl at alumni.drew.edu
Sat Apr 30 12:25:34 PDT 2011


On Sat, Apr 30, 2011 at 11:29 AM, George Hartzell <hartzell at alerce.com>wrote:

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

Sure -- imagine something like this:

package MyApp::CommandRole::Lazy;

use Moose::Role;
use namespace::autoclean;

requires '_lazy_classes';
requires 'execute';

before execute => sub { Class::MOP::load_class($_) for $_[0]->_lazy_classes
};

...then over in a command class:

package MyApp::Command::something;

use Moose;
use namespace::autoclean;
extends 'MooseX::App::Cmd::Command';
with 'MyApp::CommandRole::Lazy';

sub _lazy_classes { qw/ Big::DBIC::Schema OtherLargeClass ... / }

sub execute { ... }

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.

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.

                                    -Chris
-- 
Chris Weyl
Ex astris scientia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20110430/5ca37544/attachment.html>


More information about the SanFrancisco-pm mailing list