Note that there are some explanatory texts on larger screens.

plurals
  1. POUse a single module and get Moose plus several MooseX extensions
    primarykey
    data
    text
    <p>Let's say I have a codebase with a bunch of <a href="http://search.cpan.org/dist/Moose/" rel="nofollow">Moose</a>-based classes and I want them all to use a common set of <a href="http://search.cpan.org/search?query=MooseX&amp;mode=dist" rel="nofollow">MooseX::*</a> extension modules. But I don't want each Moose-based class to have to start like this:</p> <pre><code>package My::Class; use Moose; use MooseX::Aliases; use MooseX::HasDefaults::RO; use MooseX::StrictConstructor; ... </code></pre> <p>Instead, I want each class to begin like this:</p> <pre><code>package MyClass; use My::Moose; </code></pre> <p>and have it be exactly equivalent to the above.</p> <p>My first attempt at implementing this was based on the approach used by <a href="http://search.cpan.org/dist/Mason/lib/Mason/Moose.pm" rel="nofollow">Mason::Moose</a> (<a href="http://cpansearch.perl.org/src/JSWARTZ/Mason-2.17/lib/Mason/Moose.pm" rel="nofollow">source</a>):</p> <pre><code>package My::Moose; use Moose; use Moose::Exporter; use MooseX::Aliases(); use MooseX::StrictConstructor(); use MooseX::HasDefaults::RO(); use Moose::Util::MetaRole; Moose::Exporter-&gt;setup_import_methods(also =&gt; [ 'Moose' ]); sub init_meta { my $class = shift; my %params = @_; my $for_class = $params{for_class}; Moose-&gt;init_meta(@_); MooseX::Aliases-&gt;init_meta(@_); MooseX::StrictConstructor-&gt;init_meta(@_); MooseX::HasDefaults::RO-&gt;init_meta(@_); return $for_class-&gt;meta(); } </code></pre> <p>But this approach is not recommended by the folks in the #moose IRC channel on irc.perl.org, and it doesn't always work, depending on the mix of <code>MooseX::*</code> modules. For example, trying to use the <code>My::Moose</code> class above to make <code>My::Class</code> like this:</p> <pre><code>package My::Class; use My::Moose; has foo =&gt; (isa =&gt; 'Str'); </code></pre> <p>Results in the following error when the class is loaded:</p> <pre><code>Attribute (foo) of class My::Class has no associated methods (did you mean to provide an "is" argument?) at /usr/local/lib/perl5/site_perl/5.12.1/darwin-2level/Moose/Meta/Attribute.pm line 1020. Moose::Meta::Attribute::_check_associated_methods('Moose::Meta::Class::__ANON__::SERIAL::2=HASH(0x100bd6f00)') called at /usr/local/lib/perl5/site_perl/5.12.1/darwin-2level/Moose/Meta/Class.pm line 573 Moose::Meta::Class::add_attribute('Moose::Meta::Class::__ANON__::SERIAL::1=HASH(0x100be2f10)', 'foo', 'isa', 'Str', 'definition_context', 'HASH(0x100bd2eb8)') called at /usr/local/lib/perl5/site_perl/5.12.1/darwin-2level/Moose.pm line 79 Moose::has('Moose::Meta::Class::__ANON__::SERIAL::1=HASH(0x100be2f10)', 'foo', 'isa', 'Str') called at /usr/local/lib/perl5/site_perl/5.12.1/darwin-2level/Moose/Exporter.pm line 370 Moose::has('foo', 'isa', 'Str') called at lib/My/Class.pm line 5 require My/Class.pm called at t.pl line 1 main::BEGIN() called at lib/My/Class.pm line 0 eval {...} called at lib/My/Class.pm line 0 </code></pre> <p>The <a href="http://search.cpan.org/dist/MooseX-HasDefaults/lib/MooseX/HasDefaults.pm" rel="nofollow">MooseX::HasDefaults::RO</a> should be preventing this error, but it's apparently not being called upon to do its job. Commenting out the <code>MooseX::Aliases-&gt;init_meta(@_);</code> line "fixes" the problem, but a) that's one of the modules I want to use, and b) that just further emphasizes the wrongness of this solution. (In particular, <code>init_meta()</code> should only be called once.)</p> <p>So, I'm open to suggestions, totally ignoring my failed attempt to implement this. Any strategy is welcome as long as if gives the results described at the start of this question.</p> <hr> <p>Based on @Ether's answer, I now have the following (which also doesn't work):</p> <pre><code>package My::Moose; use Moose(); use Moose::Exporter; use MooseX::Aliases(); use MooseX::StrictConstructor(); use MooseX::HasDefaults::RO(); my %class_metaroles = ( class =&gt; [ 'MooseX::StrictConstructor::Trait::Class', ], attribute =&gt; [ 'MooseX::Aliases::Meta::Trait::Attribute', 'MooseX::HasDefaults::Meta::IsRO', ], ); my %role_metaroles = ( role =&gt; [ 'MooseX::Aliases::Meta::Trait::Role' ], application_to_class =&gt; [ 'MooseX::Aliases::Meta::Trait::Role::ApplicationToClass' ], application_to_role =&gt; [ 'MooseX::Aliases::Meta::Trait::Role::ApplicationToRole' ], ); if (Moose-&gt;VERSION &gt;= 1.9900) { push(@{$class_metaroles{class}}, 'MooseX::Aliases::Meta::Trait::Class'); push(@{$role_metaroles{applied_attribute}}, 'MooseX::Aliases::Meta::Trait::Attribute', 'MooseX::HasDefaults::Meta::IsRO'); } else { push(@{$class_metaroles{constructor}}, 'MooseX::StrictConstructor::Trait::Method::Constructor', 'MooseX::Aliases::Meta::Trait::Constructor'); } *alias = \&amp;MooseX::Aliases::alias; Moose::Exporter-&gt;setup_import_methods( also =&gt; [ 'Moose' ], with_meta =&gt; ['alias'], class_metaroles =&gt; \%class_metaroles, role_metaroles =&gt; \%role_metaroles, ); </code></pre> <p>With a sample class like this:</p> <pre><code>package My::Class; use My::Moose; has foo =&gt; (isa =&gt; 'Str'); </code></pre> <p>I get this error:</p> <pre><code>Attribute (foo) of class My::Class has no associated methods (did you mean to provide an "is" argument?) at ... </code></pre> <p>With a sample class like this:</p> <pre><code>package My::Class; use My::Moose; has foo =&gt; (isa =&gt; 'Str', alias =&gt; 'bar'); </code></pre> <p>I get this error:</p> <pre><code>Found unknown argument(s) passed to 'foo' attribute constructor in 'Moose::Meta::Attribute': alias at ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload