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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:57:03+00:00 2026-05-28T14:57:03+00:00

We use Moose classes that serialize iterators into various output formats. We describe the

  • 0

We use Moose classes that serialize iterators into various output formats. We describe the iterator as an attribute:

has iterator => (
    is => 'ro',
    isa => 'CodeRef',
    required => 1,
);

This has worked fine so far, but we have lately been using Iterator::Simple to prepare iterators for later consumption. This means that we can go about writing this:

has iterator => (
    is => 'ro',
    isa => 'CodeRef|Iterator::Simple::Iterator',
    required => 1,
);

And allow our serializers to accept the iterator class correctly. However, that seems to be a incomplete solution.

Is there a way in Moose to specify the constraint that the attribute must be callable? I suspect it may be possible with Moose::Util::TypeConstraints and using overload::Overloaded on &{} to check, but I’d like to know if anyone has created a module to do this already or if there is a Moose-standard way to test for this.

  • 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-28T14:57:04+00:00Added an answer on May 28, 2026 at 2:57 pm

    CodeRef only allows unblessed code references. Fortunately, it’s easy to make your own types.

    Define Callable as shown below, then use it instead of CodeRef. It allows the following:

    • Unblessed code references.
    • Blessed code references.
    • Objects that pretend to be code references (i.e. those that overload &{}).

    use Moose::Util::TypeConstraints;
    use overload     qw( );
    use Scalar::Util qw( );
    
    subtype 'Callable'
        => as 'Ref'
        => where {
              Scalar::Util::reftype($_) eq 'CODE'
                 ||
              Scalar::Util::blessed($_) && overload::Method($_, "&{}")
           }
    
        # Written such that parent's inline_as needs not be prepended.
        => inline_as {'(
              (Scalar::Util::reftype('.$_[1].') // "") eq 'CODE'
                 ||
              Scalar::Util::blessed('.$_[1].') && overload::Method('.$_[1].', "&{}")
           )'};
    
    no Moose::Util::TypeConstraints;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use Moose; extends 'TEST::Role'; has 'name' => ( is => 'ro', isa => 'Str',
Suppose i have the following Moose package: package GSM::Cell; use Moose; has 'ID' =>
Consider the following code: package Test1; use Moose; has 'something' => ( is =>
I am editing packages that use Moose, and I was wondering if there were
Consider this simple class: package Foo; use Moose; has foo => ( is =>
package testDB; use Moose; use Carp; use SQL::Library; has 'lib' => (#FOLDBEG is =>
Consider the following: package MyApp::CGI; use Moose; use MooseX::NonMoose; use Data::Dumper; extends 'CGI::Application'; BEGIN
I have an application that runs continuously, it creates and destroys classes some of
I was trying to overload classes to strings in perl. For example: use MooseX::Declare;
The following code defines two classes ( DeckA and DeckB ) that differ only

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.