in reply to Moose Attribute constraint: Regex OR undef

Maybe you need Maybe?

subtype 'MyMatch', as 'Str', where { /$regEx/ }, message { "$_ didn't match" }; has 'attr' => ( is => 'ro', isa => 'Maybe[MyMatch]', required => 0 );
See doc:
The Maybe[`a] type deserves a special mention. Used by itself, it doesn't really mean anything (and is equivalent to Item). When it is parameterized, it means that the value is either undef or the parameterized type. So Maybe[Int] means an integer or undef.

Hope this helps!

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Moose Attribute constraint: Regex OR undef
by hoppfrosch (Scribe) on Nov 01, 2016 at 09:26 UTC
    Silly me: I was close to the same solution - but I failed due to the usage of round brackets instead of square brackets with Maybe[....]
    Thanks!