use v5.22; use warnings; use Test::More; use Test::Deep; use Test::Warnings; use lib qw(lib t); use Type::Tiny; use Types::Standard qw( Maybe Str ); use Types::Common::String qw( NonEmptyStr ); use Type::Params qw( compile_named ); =head2 The goal is to allow a "caption", if defined, should be less than $capsize. If caption == '', I'd like to return the value as undef. =cut my $capsize = 2781; # This will warn when $_ in not numeric my $LongCaption = Type::Tiny->new( name => 'Caption', constraint => sub { length($_) < $capsize }, message => sub { "Caption needs to be less than $capsize characters" } ); my $EmptyStr = "Type::Tiny"->new( name => 'EmptyString', constraint => sub { $_ eq '' }, message => sub { "An empty string" } ); $LongCaption->plus_coercions( $EmptyStr => sub { warn "Coercing"; return undef; }, ); state $check = Type::Params::compile_named( caption => Maybe[$LongCaption], ); my $text = ''; my $val = $LongCaption->assert_return( $text ); note $val; is($val, undef, 'coerced into undef'); my $data = $check->( { caption => $text } ); cmp_deeply($data, { caption => undef }, 'coerced into undef'); done_testing;