Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:33:45+00:00 2026-06-02T05:33:45+00:00

Let’s say I have a codebase with a bunch of Moose -based classes and

  • 0

Let’s say I have a codebase with a bunch of Moose-based classes and I want them all to use a common set of MooseX::* extension modules. But I don’t want each Moose-based class to have to start like this:

package My::Class;

use Moose;
use MooseX::Aliases;
use MooseX::HasDefaults::RO;
use MooseX::StrictConstructor;
...

Instead, I want each class to begin like this:

package MyClass;

use My::Moose;

and have it be exactly equivalent to the above.

My first attempt at implementing this was based on the approach used by Mason::Moose (source):

package My::Moose;

use Moose;
use Moose::Exporter;
use MooseX::Aliases();
use MooseX::StrictConstructor();
use MooseX::HasDefaults::RO();
use Moose::Util::MetaRole;

Moose::Exporter->setup_import_methods(also => [ 'Moose' ]);

sub init_meta {
    my $class = shift;
    my %params = @_;

    my $for_class = $params{for_class};

    Moose->init_meta(@_);
    MooseX::Aliases->init_meta(@_);
    MooseX::StrictConstructor->init_meta(@_);
    MooseX::HasDefaults::RO->init_meta(@_);

    return $for_class->meta();
}

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 MooseX::* modules. For example, trying to use the My::Moose class above to make My::Class like this:

package My::Class;

use My::Moose;

has foo => (isa => 'Str');

Results in the following error when the class is loaded:

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

The MooseX::HasDefaults::RO should be preventing this error, but it’s apparently not being called upon to do its job. Commenting out the MooseX::Aliases->init_meta(@_); 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, init_meta() should only be called once.)

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.


Based on @Ether’s answer, I now have the following (which also doesn’t work):

package My::Moose;

use Moose();
use Moose::Exporter;
use MooseX::Aliases();
use MooseX::StrictConstructor();
use MooseX::HasDefaults::RO();

my %class_metaroles = (
    class => [
        'MooseX::StrictConstructor::Trait::Class',
    ],

    attribute => [
        'MooseX::Aliases::Meta::Trait::Attribute', 
        'MooseX::HasDefaults::Meta::IsRO',
     ],
);

my %role_metaroles = (
    role =>
        [ 'MooseX::Aliases::Meta::Trait::Role' ],
    application_to_class =>
        [ 'MooseX::Aliases::Meta::Trait::Role::ApplicationToClass' ],
    application_to_role =>
        [ 'MooseX::Aliases::Meta::Trait::Role::ApplicationToRole' ],
);

if (Moose->VERSION >= 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 = \&MooseX::Aliases::alias;

Moose::Exporter->setup_import_methods(
    also => [ 'Moose' ],
    with_meta => ['alias'],
    class_metaroles => \%class_metaroles,
    role_metaroles => \%role_metaroles,
);

With a sample class like this:

package My::Class;

use My::Moose;

has foo => (isa => 'Str');

I get this error:

Attribute (foo) of class My::Class has no associated methods (did you mean to provide an "is" argument?) at ...

With a sample class like this:

package My::Class;

use My::Moose;

has foo => (isa => 'Str', alias => 'bar');

I get this error:

Found unknown argument(s) passed to 'foo' attribute constructor in 'Moose::Meta::Attribute': alias at ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:33:46+00:00Added an answer on June 2, 2026 at 5:33 am

    As discussed, you shouldn’t be calling other extensions’ init_meta methods directly. Instead, you should essentially inline those extensions’ init_meta methods: combine what all those methods do, into your own init_meta. This is fragile because now you are tying your module to other modules’ innards, which are subject to change at any time.

    e.g. to combine MooseX::HasDefaults::IsRO, MooseX::StrictConstructor and MooseX::Aliases, you’d do something like this (warning: untested) (now tested!):

    package Mooseish;
    
    use Moose ();
    use Moose::Exporter;
    use MooseX::StrictConstructor ();
    use MooseX::Aliases ();
    
    my %class_metaroles = (
        class => ['MooseX::StrictConstructor::Trait::Class'],
        attribute => [
            'MooseX::Aliases::Meta::Trait::Attribute',
            'MooseX::HasDefaults::Meta::IsRO',
        ],
    );
    my %role_metaroles = (
        role =>
            ['MooseX::Aliases::Meta::Trait::Role'],
        application_to_class =>
            ['MooseX::Aliases::Meta::Trait::Role::ApplicationToClass'],
        application_to_role =>
            ['MooseX::Aliases::Meta::Trait::Role::ApplicationToRole'],
    );
    
    if (Moose->VERSION >= 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 = \&MooseX::Aliases::alias;
    
    Moose::Exporter->setup_import_methods(
        also => ['Moose'],
        with_meta => ['alias'],
        class_metaroles => \%class_metaroles,
        role_metaroles => \%role_metaroles,
    );
    
    1;
    

    This can be tested with this class and tests:

    package MyObject;
    use Mooseish;
    
    sub foo { 1 }
    
    has this => (
        isa => 'Str',
        alias => 'that',
    );
    
    1;
    

    use strict;
    use warnings;
    use MyObject;
    use Test::More;
    use Test::Fatal;
    
    like(
        exception { MyObject->new(does_not_exist => 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->new(this => 'thing');
    is($obj->that, 'thing', 'can access attribute by its aliased name');
    
    like(
        exception { $obj->this('new value') },
        qr/Cannot assign a value to a read-only accessor/,
        'attribute defaults to read-only',
    );
    
    done_testing;
    

    Which prints:

    ok 1 - strict constructor behaviour is present
    ok 2 - MyObject->can(...)
    ok 3 - can access attribute by its aliased name
    ok 4 - attribute defaults to read-only
    1..4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a model that handles recipes, and I want to
Let's say I have this string which I want to put in a multidimensional
Let's say that I have a set of relations that looks like this: relations
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I have a drive such as C:\ , and I want to
Let's say I have an string variable called *magic_string* which value is set to
Let's say I want to have some kind of a cache that did something
Let's say we have 3 classes: A, B and C. Each class has the
let's say I have a class that does some calculations. This set of calculations
Let's say i have two tables in db: Car and Part. Car owns arbitrialy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.