.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Type::Tiny::ConstrainedObject 3" .TH Type::Tiny::ConstrainedObject 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Type::Tiny::ConstrainedObject \- shared behavour for Type::Tiny::Class, etc .SH STATUS .IX Header "STATUS" This module is considered experiemental. .SH DESCRIPTION .IX Header "DESCRIPTION" .SS Methods .IX Subsection "Methods" The following methods exist for Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, and any type constraints that inherit from \&\f(CW\*(C`Object\*(C'\fR or \f(CW\*(C`Overload\*(C'\fR in Types::Standard. .PP These methods will also work for Type::Tiny::Intersection if at least one of the types in the intersection provides these methods. .PP These methods will also work for Type::Tiny::Union if all of the types in the union provide these methods. .ie n .IP stringifies_to($constraint) 4 .el .IP \f(CWstringifies_to($constraint)\fR 4 .IX Item "stringifies_to($constraint)" Generates a new child type constraint which checks the object's stringification against a constraint. For example: .Sp .Vb 2 \& my $type = Type::Tiny::Class\->new(class => \*(AqURI\*(Aq); \& my $child = $type\->stringifies_to( StrMatch[qr/^http:/] ); \& \& $child\->assert_valid( URI\->new("http://example.com/") ); .Ve .Sp In the above example, \f(CW$child\fR is a type constraint that checks objects are blessed into (or inherit from) the URI class, and when stringified (e.g. though overloading) the result matches the regular expression \f(CW\*(C`qr/^http:/\*(C'\fR. .Sp \&\f(CW$constraint\fR may be a type constraint, something that can be coerced to a type constraint (such as a coderef returning a boolean), a string of Perl code operating on \f(CW$_\fR, or a reference to a regular expression. .Sp So the following would work: .Sp .Vb 3 \& my $child = $type\->stringifies_to( sub { qr/^http:/ } ); \& my $child = $type\->stringifies_to( qr/^http:/ ); \& my $child = $type\->stringifies_to( \*(Aqm/^http:/\*(Aq ); \& \& my $child = $type\->where(\*(Aq"$_" =~ /^http:/\*(Aq); .Ve .ie n .IP numifies_to($constraint) 4 .el .IP \f(CWnumifies_to($constraint)\fR 4 .IX Item "numifies_to($constraint)" The same as \f(CW\*(C`stringifies_to\*(C'\fR but checks numification. .Sp The following might be useful: .Sp .Vb 2 \& use Types::Standard qw(Int Overload); \& my $IntLike = Int | Overload\->numifies_to(Int) .Ve .ie n .IP """with_attribute_values($attr1 => $constraint1, ...)""" 4 .el .IP "\f(CWwith_attribute_values($attr1 => $constraint1, ...)\fR" 4 .IX Item "with_attribute_values($attr1 => $constraint1, ...)" This is best explained with an example: .Sp .Vb 1 \& use Types::Common qw( InstanceOf StrMatch IntRange ); \& \& my $person = InstanceOf[\*(AqLocal::Human\*(Aq]; \& my $woman = $person\->with_attribute_values( \& gender => StrMatch[ qr/^F/i ], \& age => IntRange[ 18 => () ], \& ); \& \& $woman\->assert_valid($alice); .Ve .Sp This assertion will firstly check that \f(CW$alice\fR is a Local::Human, then check that \f(CW\*(C`$alice\->gender\*(C'\fR starts with an "F", and lastly check that \f(CW\*(C`$alice\->age\*(C'\fR is an integer at least 18. .Sp Again, constraints can be type constraints, coderefs, strings of Perl code, or regular expressions. .Sp Technically the "attributes" don't need to be Moo/Moose/Mouse attributes, but any methods which can be called with no parameters and return a scalar. .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Type::Tiny::Manual. .PP Type::Tiny. .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2019\-2023 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "DISCLAIMER OF WARRANTIES" THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.