Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Coerce or transform with Types::Param

by 1nickt (Canon)
on Mar 19, 2021 at 21:03 UTC ( [id://11129983]=note: print w/replies, xml ) Need Help??


in reply to Coerce or transform with Types::Param

Hi, I have not been able to solve your coercion problem yet, but you might be interested in Types::Common::String (which comes bundled with Type::Tiny) for working with strings, string lengths, etc.

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Coerce or transform with Types::Param
by tomred (Acolyte) on Mar 20, 2021 at 13:04 UTC
    I am familair with and did use StrLength for this code but I was not sure if it inherited from NonEmptyStr which would cause the data to fail validation, StrLenght[1, 2871] might work but if the value meets the constraint, there is no way to coerce it.

    Dermot

      I believe it inherits from SimpleStr which allows zero length.

      FWIW here's my code, which exercises the coercion when the string is empty, but does not seem to apply it. ( Update: added debug and showed output )

      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); }

      This outputs:

      Use of uninitialized value in sprintf at 11130005.pl line 20. undef : >< coercing at 11130005.pl line 10. empty : >< abcd : >abcd< coercing at 11130005.pl line 10. Value "abcde" did not pass type constraint "Undef|__ANON__" (in $_[0]) + at 11130005.pl line 18 "Undef|__ANON__" requires that the value pass "Undef" or "__ANON__ +" Value "abcde" did not pass type constraint "Undef" Value "abcde" did not pass type constraint "Undef" "Undef" is defined as: (!defined($_)) Value "abcde" did not pass type constraint "__ANON__" is a subtype of "SimpleStr&StrLength[1,4]" "SimpleStr&StrLength[1,4]" requires that the value pass "Simpl +eStr" and "StrLength[1,4]" Value "abcde" did not pass type constraint "StrLength[1,4]" "StrLength[1,4]" expects length($_) to be between 1 and 4 length($_) is 5

      Hope this helps!


      The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-28 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found