Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As discussed, you shouldn't be calling other extensions' <code>init_meta</code> methods directly. Instead, you should essentially <em>inline</em> those extensions' <code>init_meta</code> methods: combine what all those methods do, into your own <code>init_meta</code>. This is fragile because now you are tying your module to other modules' innards, which are subject to change at any time.</p> <p>e.g. to combine <a href="https://metacpan.org/module/MooseX%3a%3aHasDefaults" rel="nofollow">MooseX::HasDefaults::IsRO</a>, <a href="http://metacpan.org/module/MooseX%3a%3aStrictConstructor" rel="nofollow">MooseX::StrictConstructor</a> and <a href="http://metacpan.org/module/MooseX%3a%3aAliases" rel="nofollow">MooseX::Aliases</a>, you'd do something like this <del>(warning: untested)</del> (now tested!):</p> <pre><code>package Mooseish; use Moose (); use Moose::Exporter; use MooseX::StrictConstructor (); use MooseX::Aliases (); 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'; } 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, ); 1; </code></pre> <p>This can be tested with this class and tests:</p> <pre><code>package MyObject; use Mooseish; sub foo { 1 } has this =&gt; ( isa =&gt; 'Str', alias =&gt; 'that', ); 1; </code></pre> <hr> <pre><code>use strict; use warnings; use MyObject; use Test::More; use Test::Fatal; like( exception { MyObject-&gt;new(does_not_exist =&gt; 1) }, qr/unknown attribute.*does_not_exist/, 'strict constructor behaviour is present', ); can_ok('MyObject', qw(alias this that has with foo)); my $obj = MyObject-&gt;new(this =&gt; 'thing'); is($obj-&gt;that, 'thing', 'can access attribute by its aliased name'); like( exception { $obj-&gt;this('new value') }, qr/Cannot assign a value to a read-only accessor/, 'attribute defaults to read-only', ); done_testing; </code></pre> <p>Which prints:</p> <pre><code>ok 1 - strict constructor behaviour is present ok 2 - MyObject-&gt;can(...) ok 3 - can access attribute by its aliased name ok 4 - attribute defaults to read-only 1..4 </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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