The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


MooseX::NotRequired - Make Moose sub classes with non required attributes.


Version 0.12


This module allows you to create anonymous sub classes of Moose classes with all the required flags on the attributes turned off.

    package SalesOrder;
    use Moose;

    has order_number    => (is => 'ro', isa => 'Str', required => 1);
    has reference       => (is => 'ro', isa => 'Str' );
    has date_ordered    => (is => 'ro', isa => 'DateTime', required => 1);
    has total_value     => (is => 'ro', isa => 'Int', required => 1);
    has customer        => (is => 'ro', isa => 'Str', required => 1);
    has notes           => (is => 'ro', isa => 'Str');



    use MooseX::NotRequired;

    my $new_class = MooseX::NotRequired::make_optional_subclass('SalesOrder');
    my $obj = $new_class->new(); # no blow up
    my $default = $new_class->new({ semi_required => undef }); # fine too
    my $second = ObjectA->new(); # blow up because required fields not present
    my $third = ObjectA->new({ order_number => 'a', semi_required => undef }); 
    # blow up because semi_required must be a string.

This module exists because while you want to make use of Moose's awesome type constraints they're sometimes a little inconvenient. Rather than throw out all your restrictions because you need your class to be a little more permissive in a few scenarios, create a subclass that has some of those restrictions weakened. You can of course do this manually.



This creates an anonymous sub class that has all the required flags on the attributes removed. It also turns type constraints into Maybe constraints where possible. That generally only works for simple isa's like 'Str'.


Colin Newell, <colin.newell at>


Please report any bugs or feature requests to I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc MooseX::NotRequired

You can also look for information at:


This could wouldn't be possible without the help of doy on #moose. The cool bits were written by him and the bugs/bad practice added by Colin. JJ also helped get things going again when I got stuck. The Birmingham Perl Mongers also provided input which helped refine and improve the module.


Copyright 2011-2015 OpusVL.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.