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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:41:59+00:00 2026-05-21T21:41:59+00:00

Moose types are great, but sometimes you need to be more specific. You all

  • 0

Moose types are great, but sometimes you need to be more specific. You all know these data type rules: that parameter may only be 'A', 'B' or 'C', or only a currency symbol, or must conform to some regular expression.

Take a look at the following example which has two constrained attributes, one must be either 'm' or 'f', the other must be a valid ISO date. What’s the best way in Moose to specify these constraints? I’d think of the SQL CHECK clause, but AFAICS there is no check keyword in Moose. So I used trigger, but it sounds wrong. Anyone has a better answer?

package Person;
use Moose;

has gender          => is => 'rw', isa => 'Str', trigger =>
    sub { confess 'either m or f' if $_[1] !~ m/^m|f$/ };
has name            => is => 'rw', isa => 'Str';
has dateOfBirth     => is => 'rw', isa => 'Str', trigger =>
    sub { confess 'not an ISO date' if $_[1] !~ m/^\d\d\d\d-\d\d-\d\d$/ };

no Moose;
__PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;

dies_ok { Person->new( gender => 42 ) } 'gender must be m or f';
dies_ok { Person->new( dateOfBirth => 42 ) } 'must be an ISO date';

done_testing;

Here’s what I wound up using:

package Blabla::Customer;
use Moose::Util::TypeConstraints;
use Moose;

subtype ISODate => as 'Str' => where { /^\d\d\d\d-\d\d-\d\d$/ };

has id              => is => 'rw', isa => 'Str';
has gender          => is => 'rw', isa => enum ['m', 'f'];
has firstname       => is => 'rw', isa => 'Str';
has dateOfBirth     => is => 'rw', isa => 'ISODate';

no Moose;
__PACKAGE__->meta->make_immutable;

This is Moose version 1.19, in case it matters. I got the following warning for the wrong subtype as => 'Str', where => { ... } syntax I erroneously introduced: Calling subtype() with a simple list of parameters is deprecated. So I had to change it a bit according to the fine manual.

  • 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-21T21:42:00+00:00Added an answer on May 21, 2026 at 9:42 pm

    Just define your own subtype, and use that.

    package Person;
    
    use Moose::Util::TypeConstraints;
    
    use namespace::clean;
    use Moose;
    
    has gender => (
      is => 'rw',
      isa => subtype(
        as 'Str',
        where { /^[mf]$/ }
      ),
    );
    has name => (
      is => 'rw',
      isa => 'Str'
    );
    has dateOfBirth => (
      is => 'rw',
      isa => subtype(
        as 'Str',
        where { /^\d\d\d\d-\d\d-\d\d$/ }
      ),
    );
    
    no Moose;
    __PACKAGE__->meta->make_immutable;
    1;
    
    package main;
    use Test::More;
    use Test::Exception;
    
    dies_ok { Person->new( gender => 42 ) } 'gender must be m or f';
    dies_ok { Person->new( dateOfBirth => 42 ) } 'must be an ISO date';
    
    done_testing;
    

    Or you could use the MooseX::Types module.

    package Person::TypeConstraints;
    
    use MooseX::Types::Moose qw'Str';
    use MooseX::Types -declare => [qw'
      Gender ISODate
    '];
    
    subtype Gender, (
      as Str,
      where { /^[mf]$/ },
    );
    
    subtype ISODate, (
      as Str,
      where { /^\d\d\d\d-\d\d-\d\d$/ }
    );
    1;
    
    package Person:
    
    use MooseX::Types::Moose qw'Str';
    use Person::TypeConstraints qw'Gender ISODate';
    
    use namespace::clean;
    use Moose;
    
    has gender => (
      is => 'rw',
      isa => Gender,
    );
    has name => (
      is => 'rw',
      isa => Str,
    );
    has dateOfBirth => (
      is => 'rw',
      isa => ISODate,
    );
    
    no Moose;
    __PACKAGE__->meta->make_immutable;
    1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a structured type in Moose that can be used as
I'm using Moose and I need to wrap method calls in my project. It's
I've got a Perl Moose object that contains an attribute I'd like to use
I need multemethod dispatch with Moose objects. I'm doing this with Class::Multimethods::Pure . I
I have a small Java desktop app that uses Swing. There is a data
I developed something in flex. It works great on FF and Safari. But on
I've searched the internet, all previous answers said that a semicolon is missing. That's
This follows on from my previous question about Moose structured types. I apologise for
Is there anyway to get $self into a MooseX::Types coercion? I have other data
Here's a tooltip script from webdesign tuts+ . Works great, but it fails to

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.