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

  • Home
  • SEARCH
  • 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 1066237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:59:11+00:00 2026-05-16T19:59:11+00:00

let’s say you’ve got a Moose class that requires an attribute that does a

  • 0

let’s say you’ve got a Moose class that requires an attribute that does a Role:

package MyMooseClass;
use Moose;

has 'a' => ( does => 'MyRole' );

Now, I’d like to build an instance of MyMooseClass like this:

my $instance = MyMooseClass->new( { a => $a_non_moose_stuff } );

Where $a_non_moose_stuff is an instance of a non-moose class, but that implements the required role methods.

Is there a way to have Moose to check that my $a_non_moose_stuff complies with the role, even though it’s not implemented using Moose?

  • 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-16T19:59:11+00:00Added an answer on May 16, 2026 at 7:59 pm

    The easiest thing is to use a duck_type instead of a role to validate your interface. A duck_type is a looser restriction, basically the duck_type is just a list of methods the object is expected to have. For example:

    package MyMooseClass;
    use Moose;
    use Moose::Util::TypeConstraints qw/duck_type/;
    
    has 'a' => (
      isa => duck_type(qw/method1 method1 some_other_method/),
    );
    

    See Moose::Util::TypeConstraints and scroll down a bit til you see the entry for duck_type. This constraint was added specifically to deal with this kind of issue.

    If you are going to reuse this constraint a lot, you might want to look at creating it in a type library class, which will promote reuse. Check out MooseX::Types.

    BTW, although using a Hash Reference in the constructor is supported, and for a long time prior to Moose considered a best practice, most Moose authors I know skip it and just supply a list of constructor parameters. Its a bit less typing and looks cleaner, in my opinion:

    my $instance = MyClass->new(param1=>'val1', param2=>'val2');
    

    The main point of the Hash Reference approach was to help solve some ambiguities that really don’t happen when you are using Moose to construct your Perl object. Since Moose deals with all that boilerplate and ceremony for you, it is unnecessary in my opinion, although styles do vary. My two cents.

    Take it easy,

    John

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say you have a class, with certain properties, and that you tried your
Let's say I've got a collection of 10 million documents that look something like
Let's say I've got n properties (pairs key-value) that are either dates, numbers or
Let's say that I have a SQLite database that I create in a separate
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say I have the following classes : public class MyProductCode { private String

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.