use strict; use warnings; use feature 'say', 'state'; use Types::Standard 'Undef'; use Types::Common::String 'SimpleStr','StrLength'; use Type::Params 'compile'; state $check = compile ( Undef | (SimpleStr & (StrLength[1,4]))->plus_coercions( SimpleStr, sub { warn 'coercing'; length ? $_ : undef }, ) ); #no warnings 'uninitialized'; for my $str ( undef, '', 'abcd', 'abcde' ) { my $label = ! defined($str) ? 'undef' : ! length ($str) ? 'empty' : $str; my $checked = $check->($str); say sprintf('%s : >%s<', $label, $str); }