# in package MyModule::Types declare DBPrefix, as Optional[StrMatch[ qr/ \w+_ /x ]], message { 'The table prefix must end in an underscore. ' }; # in package MyModule use Type::Params qw/ compile /; use My::Types qw/ DBPrefix /; state $validate = compile( DBPrefix ); my ($param) = $validate->( @_ ); # in foo.pl $obj->method( 'bar' ); # output: # The table prefix must end in an underscore. (in $_[0]) at ./foo.pl line 11 # "DBPrefix" is a subtype of "Optional[StrMatch[(?^x: \w+_ )]]" # Value "bar" did not pass type constraint "Optional[StrMatch[(?^x: \w+_ )]]" (in $_[0]) # $_[0] exists # "Optional[StrMatch[(?^x: \w+_ )]]" constrains $_[0] with "StrMatch[(?^x: \w+_ )]" if it exists # Value "bar" did not pass type constraint "StrMatch[(?^x: \w+_ )]" (in $_[0]) # "StrMatch[(?^x: \w+_ )]" is defined as: do { !ref($_) and $_ =~ $Types::Standard::_StrMatch{"(?^x: \\w+_ )"} } #### state $validate = compile(slurpy Dict[ prefix => DBPrefix ]); #per the docs for named args my ($param) = $validate->( @_ ); # in foo.pl $obj->method( 'bar' ); # output : # Reference {"prefix" => "bar"} did not pass type constraint "Dict[prefix=>DBPrefix]" (in $SLURPY) at ./foo.pl line 11