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 462437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:04:28+00:00 2026-05-12T23:04:28+00:00

I’m using Moose roles to apply some wrapper behaviour around some accessor methods in

  • 0

I’m using Moose roles to apply some wrapper behaviour around some accessor methods in a class. I want to apply this role to a number of modules, each of which have a different set of attributes whose accessors I want to wrap. Is there a way to access the meta class of the module being applied to, from within the role? i.e. something like this:

package My::Foo;
use Moose;
with 'My::Role::X';

has [ qw(attr1 attr2) ] => (
    is => 'rw', # ...
);

has 'fields' => (
    is => 'bare', isa => 'ArrayRef[Str]',
    default => sub { [qw(attr1 attr2) ] },
);
1;

package My::Role::X;
use Moose::Role;

# this should be a Moose::Meta::Class object
my $target_meta = '????';

# get Class::MOP::Attribute object out of the metaclass
my $fields_attr = $target_meta->find_attribute_by_name('fields');

# extract the value of this attribute - should be a coderef
my $fields_to_modify = $fields_attr->default;

# evaluate the coderef to get the arrayref
$fields_to_modify = &$fields_to_modify if ref $fields_to_modify eq 'CODE';

around $_ => sub {
    # ...
} for @$fields_to_modify;
1;
  • 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-05-12T23:04:28+00:00Added an answer on May 12, 2026 at 11:04 pm

    It looks like MooseX::Role::Parameterized will do the trick:

    Ordinary roles can require that its consumers have a particular list of method names. Since parameterized roles have direct access to its consumer, you can inspect it and throw errors if the consumer does not meet your needs. (link)

    The details of the role specialization is kept from the class being augmented; it doesn’t even need to pass any parameters all it needs to know is what parameters (the list of fields to wrap) to pass to the role. The only key is that the role must be used after the relevant attributes have been defined on the class.

    Therefore, the consumed class and the role become defined like so:

    package My::Foo;
    use Moose;
    
    my @fields = qw(attr1 attr2);
    
    has \@fields => (
        is => 'rw', # ...
    );
    
    has 'fields' => (
        is => 'bare', isa => 'ArrayRef[Str]',
        default => sub { \@fields },
    );
    
    with 'My::Role::X' => {};
    
    1;
    
    package My::Role::X;
    use MooseX::Role::Parameterized;
    
    role {
        my $p = shift;
    
        my %args = @_;
    
        # this should be a Moose::Meta::Class object
        my $target_meta = $args{consumer};
    
        # get Class::MOP::Attribute object out of the metaclass
        my $fields_attr = $target_meta->find_attribute_by_name('fields');
    
        # extract the value of this attribute - should be a coderef
        my $fields_to_modify = $fields_attr->default;
    
        # evaluate the coderef to get the arrayref
        $fields_to_modify = &$fields_to_modify if ref $fields_to_modify eq 'CODE';
    
        around $_ => sub {
            # ...
        } for @$fields_to_modify;
    };
    
    1;
    

    Addendum: I have discovered that if a parameterized role consumes another parameterized role, then $target_meta in the nested role will actually be the meta-class of the parent role (isa MooseX::Role::Parameterized::Meta::Role::Parameterized), rather than the meta-class of the consuming class (isa Moose::Meta::Class). In order for the proper meta-class to be derived, you need to explicitly pass it as a parameter. I have added this to all my parameterized roles as a “best practice” template:

    package MyApp::Role::SomeRole;
    
    use MooseX::Role::Parameterized;
    
    # because we are used by an earlier role, meta is not actually the meta of the
    # consumer, but of the higher-level parameterized role.
    parameter metaclass => (
        is => 'ro', isa => 'Moose::Meta::Class',
        required => 1,
    );
    
    # ... other parameters here...
    
    role {
        my $params = shift;
        my %args = @_;
    
        # isa a Moose::Meta::Class
        my $meta = $params->metaclass;
    
        # class name of what is consuming us, om nom nom
        my $consumer = $meta->name;
    
        # ... code here...
    
    }; # end role
    no Moose::Role;
    1;
    

    Addendum 2: I have further discovered that if the role is being applied to an object instance, as opposed to a class, then $target_meta in the role will actually be the class of the object doing the consuming:

    package main;
    use My::Foo;
    use Moose::Util;
    
    my $foo = My::Foo->new;
    Moose::Util::apply_all_roles($foo, MyApp::Role::SomeRole, { parameter => 'value' });
    
    package MyApp::Role::SomeRole;
    use MooseX::Role::Parameterized;
    # ... use same code as above (in addendum 1):
    
    role {
        my $meta = $args{consumer};
        my $consumer = $meta->name;     # fail! My::Foo does not implement the 'name' method
    

    Therefore, this code is necessary when extracting the meta-class at the start of the parameterized role:

    role {
        my $params = shift;
        my %args = @_;
    
        # could be a Moose::Meta::Class, or the object consuming us
        my $meta = $args{consumer};
        $meta = $meta->meta if not $meta->isa('Moose::Meta::Class');   # <-- important!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I use (heavyweight) checkouts in Bazaar, so I'm not sure… May 15, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Update: These days there is Bower, Component and Browserify which… May 15, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Use this: =RIGHT(A1,LEN(A1)-FIND("OU=",A1)+1) May 15, 2026 at 8:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.