#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package Child::Creator; use MooseX::Role::Parameterized; parameter context => (isa => 'Str', required => 1); role { my ($p) = @_; my $context = $p->context; has _child_context => (is => 'ro', default => $context); sub create_child { 'Child'->new(context => shift->_child_context); } }; } { package Parent; use Moose; with 'Child::Creator' => {context => 'parent'}; __PACKAGE__->meta->make_immutable; } { package Teacher; use Moose; with 'Child::Creator' => {context => 'teacher'}; __PACKAGE__->meta->make_immutable; } { package Child; use Moose; use Moose::Util::TypeConstraints qw{ enum }; has context => (is => 'ro', required => 1, isa => enum([qw[ parent teacher ]])); __PACKAGE__->meta->make_immutable; } my $p = 'Parent'->new; my $ch_p = $p->create_child; say $ch_p->context; my $t = 'Teacher'->new; my $ch_t = $t->create_child; say $ch_t->context; # Attribute (context) is required at constructor Child::new (defined at ./1.pl line 44) line 30 my $ch = 'Child'->new; # Attribute (context) does not pass the type constraint because: Validation failed for '__ANON__' with value "unknown" at constructor Child::new (defined at ./1.pl line 44) line 39 my $ch = 'Child'->new(context => 'unknown');