1Type::Tiny(3) User Contributed Perl Documentation Type::Tiny(3)
2
3
4
6 Type::Tiny - tiny, yet Moo(se)-compatible type constraint
7
9 use v5.12;
10 use strict;
11 use warnings;
12
13 package Horse {
14 use Moo;
15 use Types::Standard qw( Str Int Enum ArrayRef Object );
16 use Type::Params qw( signature );
17 use namespace::autoclean;
18
19 has name => (
20 is => 'ro',
21 isa => Str,
22 required => 1,
23 );
24 has gender => (
25 is => 'ro',
26 isa => Enum[qw( f m )],
27 );
28 has age => (
29 is => 'rw',
30 isa => Int->where( '$_ >= 0' ),
31 );
32 has children => (
33 is => 'ro',
34 isa => ArrayRef[Object],
35 default => sub { return [] },
36 );
37
38 sub add_child {
39 state $check = signature(
40 method => Object,
41 positional => [ Object ],
42 ); # method signature
43 my ( $self, $child ) = $check->( @_ ); # unpack @_
44
45 push @{ $self->children }, $child;
46 return $self;
47 }
48 }
49
50 package main;
51
52 my $boldruler = Horse->new(
53 name => "Bold Ruler",
54 gender => 'm',
55 age => 16,
56 );
57
58 my $secretariat = Horse->new(
59 name => "Secretariat",
60 gender => 'm',
61 age => 0,
62 );
63
64 $boldruler->add_child( $secretariat );
65
67 This module is covered by the Type-Tiny stability policy.
68
70 This documents the internals of the Type::Tiny class.
71 Type::Tiny::Manual is a better starting place if you're new.
72
73 Type::Tiny is a small class for creating Moose-like type constraint
74 objects which are compatible with Moo, Moose and Mouse.
75
76 use Scalar::Util qw(looks_like_number);
77 use Type::Tiny;
78
79 my $NUM = "Type::Tiny"->new(
80 name => "Number",
81 constraint => sub { looks_like_number($_) },
82 message => sub { "$_ ain't a number" },
83 );
84
85 package Ermintrude {
86 use Moo;
87 has favourite_number => (is => "ro", isa => $NUM);
88 }
89
90 package Bullwinkle {
91 use Moose;
92 has favourite_number => (is => "ro", isa => $NUM);
93 }
94
95 package Maisy {
96 use Mouse;
97 has favourite_number => (is => "ro", isa => $NUM);
98 }
99
100 Type::Tiny conforms to Type::API::Constraint,
101 Type::API::Constraint::Coercible, Type::API::Constraint::Constructor,
102 and Type::API::Constraint::Inlinable.
103
104 Maybe now we won't need to have separate MooseX, MouseX and MooX
105 versions of everything? We can but hope...
106
107 Constructor
108 new(%attributes)
109 Moose-style constructor function.
110
111 Attributes
112 Attributes are named values that may be passed to the constructor. For
113 each attribute, there is a corresponding reader method. For example:
114
115 my $type = Type::Tiny->new( name => "Foo" );
116 print $type->name, "\n"; # says "Foo"
117
118 Important attributes
119
120 These are the attributes you are likely to be most interested in
121 providing when creating your own type constraints, and most interested
122 in reading when dealing with type constraint objects.
123
124 "constraint"
125 Coderef to validate a value ($_) against the type constraint. The
126 coderef will not be called unless the value is known to pass any
127 parent type constraint (see "parent" below).
128
129 Alternatively, a string of Perl code checking $_ can be passed as a
130 parameter to the constructor, and will be converted to a coderef.
131
132 Defaults to "sub { 1 }" - i.e. a coderef that passes all values.
133
134 "parent"
135 Optional attribute; parent type constraint. For example, an
136 "Integer" type constraint might have a parent "Number".
137
138 If provided, must be a Type::Tiny object.
139
140 "inlined"
141 A coderef which returns a string of Perl code suitable for inlining
142 this type. Optional.
143
144 (The coderef will be called in list context and can actually return
145 a list of strings which will be joined with "&&". If the first item
146 on the list is undef, it will be substituted with the type's
147 parent's inline check.)
148
149 If "constraint" (above) is a coderef generated via Sub::Quote, then
150 Type::Tiny may be able to automatically generate "inlined" for you.
151 If "constraint" (above) is a string, it will be able to.
152
153 "name"
154 The name of the type constraint. These need to conform to certain
155 naming rules (they must begin with an uppercase letter and continue
156 using only letters, digits 0-9 and underscores).
157
158 Optional; if not supplied will be an anonymous type constraint.
159
160 "display_name"
161 A name to display for the type constraint when stringified. These
162 don't have to conform to any naming rules. Optional; a default name
163 will be calculated from the "name".
164
165 "library"
166 The package name of the type library this type is associated with.
167 Optional. Informational only: setting this attribute does not
168 install the type into the package.
169
170 "deprecated"
171 Optional boolean indicating whether a type constraint is
172 deprecated. Type::Library will issue a warning if you attempt to
173 import a deprecated type constraint, but otherwise the type will
174 continue to function as normal. There will not be deprecation
175 warnings every time you validate a value, for instance. If omitted,
176 defaults to the parent's deprecation status (or false if there's no
177 parent).
178
179 "message"
180 Coderef that returns an error message when $_ does not validate
181 against the type constraint. Optional (there's a vaguely sensible
182 default.)
183
184 "coercion"
185 A Type::Coercion object associated with this type.
186
187 Generally speaking this attribute should not be passed to the
188 constructor; you should rely on the default lazily-built coercion
189 object.
190
191 You may pass "coercion => 1" to the constructor to inherit
192 coercions from the constraint's parent. (This requires the parent
193 constraint to have a coercion.)
194
195 "sorter"
196 A coderef which can be passed two values conforming to this type
197 constraint and returns -1, 0, or 1 to put them in order.
198 Alternatively an arrayref containing a pair of coderefs — a sorter
199 and a pre-processor for the Schwarzian transform. Optional.
200
201 The idea is to allow for:
202
203 @sorted = Int->sort( 2, 1, 11 ); # => 1, 2, 11
204 @sorted = Str->sort( 2, 1, 11 ); # => 1, 11, 2
205
206 "type_default"
207 A coderef which returns a sensible default value for this type. For
208 example, for a Counter type, a sensible default might be "0":
209
210 my $Size = Type::Tiny->new(
211 name => 'Size',
212 parent => Types::Standard::Enum[ qw( XS S M L XL ) ],
213 type_default => sub { return 'M'; },
214 );
215
216 package Tshirt {
217 use Moo;
218 has size => (
219 is => 'ro',
220 isa => $Size,
221 default => $Size->type_default,
222 );
223 }
224
225 Child types will inherit a type default from their parent unless
226 the child has a "constraint". If a type neither has nor inherits a
227 type default, then calling "type_default" will return undef.
228
229 As a special case, this:
230
231 $type->type_default( @args )
232
233 Will return:
234
235 sub {
236 local $_ = \@args;
237 $type->type_default->( @_ );
238 }
239
240 Many of the types defined in Types::Standard and other bundled type
241 libraries have type defaults, but discovering them is left as an
242 exercise for the reader.
243
244 "my_methods"
245 Experimental hashref of additional methods that can be called on
246 the type constraint object.
247
248 "exception_class"
249 The class used to throw an exception when a value fails its type
250 check. Defaults to "Error::TypeTiny::Assertion", which is usually
251 good. This class is expected to provide a "throw_cb" method
252 compatible with the method of that name in Error::TypeTiny.
253
254 If a parent type constraint has a custom "exception_class", then
255 this will be "inherited" by its children.
256
257 Attributes related to parameterizable and parameterized types
258
259 The following additional attributes are used for parameterizable (e.g.
260 "ArrayRef") and parameterized (e.g. "ArrayRef[Int]") type constraints.
261 Unlike Moose, these aren't handled by separate subclasses.
262
263 "constraint_generator"
264 Coderef that is called when a type constraint is parameterized.
265 When called, it is passed the list of parameters, though any
266 parameter which looks like a foreign type constraint (Moose type
267 constraints, Mouse type constraints, etc, and coderefs(!!!)) is
268 first coerced to a native Type::Tiny object.
269
270 Note that for compatibility with the Moose API, the base type is
271 not passed to the constraint generator, but can be found in the
272 package variable $Type::Tiny::parameterize_type. The first
273 parameter is also available as $_.
274
275 Types can be parameterized with an empty parameter list. For
276 example, in Types::Standard, "Tuple" is just an alias for
277 "ArrayRef" but "Tuple[]" will only allow zero-length arrayrefs to
278 pass the constraint. If you wish "YourType" and "YourType[]" to
279 mean the same thing, then do:
280
281 return $Type::Tiny::parameterize_type unless @_;
282
283 The constraint generator should generate and return a new
284 constraint coderef based on the parameters. Alternatively, the
285 constraint generator can return a fully-formed Type::Tiny object,
286 in which case the "name_generator", "inline_generator", and
287 "coercion_generator" attributes documented below are ignored.
288
289 Optional; providing a generator makes this type into a
290 parameterizable type constraint. If there is no generator,
291 attempting to parameterize the type constraint will throw an
292 exception.
293
294 "name_generator"
295 A coderef which generates a new display_name based on parameters.
296 Called with the same parameters and package variables as the
297 "constraint_generator". Expected to return a string.
298
299 Optional; the default is reasonable.
300
301 "inline_generator"
302 A coderef which generates a new inlining coderef based on
303 parameters. Called with the same parameters and package variables
304 as the "constraint_generator". Expected to return a coderef.
305
306 Optional.
307
308 "coercion_generator"
309 A coderef which generates a new Type::Coercion object based on
310 parameters. Called with the same parameters and package variables
311 as the "constraint_generator". Expected to return a blessed object.
312
313 Optional.
314
315 "deep_explanation"
316 This API is not finalized. Coderef used by
317 Error::TypeTiny::Assertion to peek inside parameterized types and
318 figure out why a value doesn't pass the constraint.
319
320 "parameters"
321 In parameterized types, returns an arrayref of the parameters.
322
323 Lazy generated attributes
324
325 The following attributes should not be usually passed to the
326 constructor; unless you're doing something especially unusual, you
327 should rely on the default lazily-built return values.
328
329 "compiled_check"
330 Coderef to validate a value ($_[0]) against the type constraint.
331 This coderef is expected to also handle all validation for the
332 parent type constraints.
333
334 "definition_context"
335 Hashref of information indicating where the type constraint was
336 originally defined. Type::Tiny will generate this based on "caller"
337 if you do not supply it. The hashref will ordinarily contain keys
338 "package", "file", and "line".
339
340 For parameterized types and compound types (e.g. unions and
341 intersections), this may not be especially meaningful information.
342
343 "complementary_type"
344 A complementary type for this type. For example, the complementary
345 type for an integer type would be all things that are not integers,
346 including floating point numbers, but also alphabetic strings,
347 arrayrefs, filehandles, etc.
348
349 "moose_type", "mouse_type"
350 Objects equivalent to this type constraint, but as a
351 Moose::Meta::TypeConstraint or Mouse::Meta::TypeConstraint.
352
353 It should rarely be necessary to obtain a
354 Moose::Meta::TypeConstraint object from Type::Tiny because the
355 Type::Tiny object itself should be usable pretty much anywhere a
356 Moose::Meta::TypeConstraint is expected.
357
358 Methods
359 Predicate methods
360
361 These methods return booleans indicating information about the type
362 constraint. They are each tightly associated with a particular
363 attribute. (See "Attributes".)
364
365 "has_parent", "has_library", "has_inlined", "has_constraint_generator",
366 "has_inline_generator", "has_coercion_generator", "has_parameters",
367 "has_message", "has_deep_explanation", "has_sorter"
368 Simple Moose-style predicate methods indicating the presence or
369 absence of an attribute.
370
371 "has_coercion"
372 Predicate method with a little extra DWIM. Returns false if the
373 coercion is a no-op.
374
375 "is_anon"
376 Returns true iff the type constraint does not have a "name".
377
378 "is_parameterized", "is_parameterizable"
379 Indicates whether a type has been parameterized (e.g.
380 "ArrayRef[Int]") or could potentially be (e.g. "ArrayRef").
381
382 "has_parameterized_from"
383 Useless alias for "is_parameterized".
384
385 Validation and coercion
386
387 The following methods are used for coercing and validating values
388 against a type constraint:
389
390 check($value)
391 Returns true iff the value passes the type constraint.
392
393 validate($value)
394 Returns the error message for the value; returns an explicit undef
395 if the value passes the type constraint.
396
397 assert_valid($value)
398 Like check($value) but dies if the value does not pass the type
399 constraint.
400
401 Yes, that's three very similar methods. Blame
402 Moose::Meta::TypeConstraint whose API I'm attempting to emulate.
403 :-)
404
405 assert_return($value)
406 Like assert_valid($value) but returns the value if it passes the
407 type constraint.
408
409 This seems a more useful behaviour than assert_valid($value). I
410 would have just changed assert_valid($value) to do this, except
411 that there are edge cases where it could break Moose compatibility.
412
413 get_message($value)
414 Returns the error message for the value; even if the value passes
415 the type constraint.
416
417 "validate_explain($value, $varname)"
418 Like "validate" but instead of a string error message, returns an
419 arrayref of strings explaining the reasoning why the value does not
420 meet the type constraint, examining parent types, etc.
421
422 The $varname is an optional string like '$foo' indicating the name
423 of the variable being checked.
424
425 coerce($value)
426 Attempt to coerce $value to this type.
427
428 assert_coerce($value)
429 Attempt to coerce $value to this type. Throws an exception if this
430 is not possible.
431
432 Child type constraint creation and parameterization
433
434 These methods generate new type constraint objects that inherit from
435 the constraint they are called upon:
436
437 create_child_type(%attributes)
438 Construct a new Type::Tiny object with this object as its parent.
439
440 where($coderef)
441 Shortcut for creating an anonymous child type constraint. Use it
442 like "HashRef->where(sub { exists($_->{name}) })". That said, you
443 can get a similar result using overloaded "&":
444
445 HashRef & sub { exists($_->{name}) }
446
447 Like the "constraint" attribute, this will accept a string of Perl
448 code:
449
450 HashRef->where('exists($_->{name})')
451
452 "child_type_class"
453 The class that create_child_type will construct by default.
454
455 parameterize(@parameters)
456 Creates a new parameterized type; throws an exception if called on
457 a non-parameterizable type.
458
459 of(@parameters)
460 A cute alias for "parameterize". Use it like "ArrayRef->of(Int)".
461
462 "plus_coercions($type1, $code1, ...)"
463 Shorthand for creating a new child type constraint with the same
464 coercions as this one, but then adding some extra coercions (at a
465 higher priority than the existing ones).
466
467 "plus_fallback_coercions($type1, $code1, ...)"
468 Like "plus_coercions", but added at a lower priority.
469
470 "minus_coercions($type1, ...)"
471 Shorthand for creating a new child type constraint with fewer type
472 coercions.
473
474 "no_coercions"
475 Shorthand for creating a new child type constraint with no
476 coercions at all.
477
478 Type relationship introspection methods
479
480 These methods allow you to determine a type constraint's relationship
481 to other type constraints in an organised hierarchy:
482
483 equals($other), is_subtype_of($other), is_supertype_of($other),
484 is_a_type_of($other)
485 Compare two types. See Moose::Meta::TypeConstraint for what these
486 all mean. (OK, Moose doesn't define "is_supertype_of", but you get
487 the idea, right?)
488
489 Note that these have a slightly DWIM side to them. If you create
490 two Type::Tiny::Class objects which test the same class, they're
491 considered equal. And:
492
493 my $subtype_of_Num = Types::Standard::Num->create_child_type;
494 my $subtype_of_Int = Types::Standard::Int->create_child_type;
495 $subtype_of_Int->is_subtype_of( $subtype_of_Num ); # true
496
497 strictly_equals($other), is_strictly_subtype_of($other),
498 is_strictly_supertype_of($other), is_strictly_a_type_of($other)
499 Stricter versions of the type comparison functions. These only care
500 about explicit inheritance via "parent".
501
502 my $subtype_of_Num = Types::Standard::Num->create_child_type;
503 my $subtype_of_Int = Types::Standard::Int->create_child_type;
504 $subtype_of_Int->is_strictly_subtype_of( $subtype_of_Num ); # false
505
506 "parents"
507 Returns a list of all this type constraint's ancestor constraints.
508 For example, if called on the "Str" type constraint would return
509 the list "(Value, Defined, Item, Any)".
510
511 Due to a historical misunderstanding, this differs from the Moose
512 implementation of the "parents" method. In Moose, "parents" only
513 returns the immediate parent type constraints, and because type
514 constraints only have one immediate parent, this is effectively an
515 alias for "parent". The extension module
516 MooseX::Meta::TypeConstraint::Intersection is the only place where
517 multiple type constraints are returned; and they are returned as an
518 arrayref in violation of the base class' documentation. I'm keeping
519 my behaviour as it seems more useful.
520
521 find_parent($coderef)
522 Loops through the parent type constraints including the invocant
523 itself and returns the nearest ancestor type constraint where the
524 coderef evaluates to true. Within the coderef the ancestor
525 currently being checked is $_. Returns undef if there is no match.
526
527 In list context also returns the number of type constraints which
528 had been looped through before the matching constraint was found.
529
530 "find_constraining_type"
531 Finds the nearest ancestor type constraint (including the type
532 itself) which has a "constraint" coderef.
533
534 Equivalent to:
535
536 $type->find_parent(sub { not $_->_is_null_constraint })
537
538 "coercibles"
539 Return a type constraint which is the union of type constraints
540 that can be coerced to this one (including this one). If this type
541 constraint has no coercions, returns itself.
542
543 "type_parameter"
544 In parameterized type constraints, returns the first item on the
545 list of parameters; otherwise returns undef. For example:
546
547 ( ArrayRef[Int] )->type_parameter; # returns Int
548 ( ArrayRef[Int] )->parent; # returns ArrayRef
549
550 Note that parameterizable type constraints can perfectly
551 legitimately take multiple parameters (several of the
552 parameterizable type constraints in Types::Standard do). This
553 method only returns the first such parameter. "Attributes related
554 to parameterizable and parameterized types" documents the
555 "parameters" attribute, which returns an arrayref of all the
556 parameters.
557
558 "parameterized_from"
559 Harder to spell alias for "parent" that only works for
560 parameterized types.
561
562 Hint for people subclassing Type::Tiny: Since version 1.006000, the
563 methods for determining subtype, supertype, and type equality should
564 not be overridden in subclasses of Type::Tiny. This is because of the
565 problem of diamond inheritance. If X and Y are both subclasses of
566 Type::Tiny, they both need to be consulted to figure out how type
567 constraints are related; not just one of them should be overriding
568 these methods. See the source code for Type::Tiny::Enum for an example
569 of how subclasses can give hints about type relationships to
570 Type::Tiny. Summary: push a coderef onto @Type::Tiny::CMP. This
571 coderef will be passed two type constraints. It should then return one
572 of the constants Type::Tiny::CMP_SUBTYPE (first type is a subtype of
573 second type), Type::Tiny::CMP_SUPERTYPE (second type is a subtype of
574 first type), Type::Tiny::CMP_EQUAL (the two types are exactly the
575 same), Type::Tiny::CMP_EQUIVALENT (the two types are effectively the
576 same), or Type::Tiny::CMP_UNKNOWN (your coderef couldn't establish any
577 relationship).
578
579 Type relationship introspection function
580
581 "Type::Tiny::cmp($type1, $type2)"
582 The subtype/supertype relationship between types results in a
583 partial ordering of type constraints.
584
585 This function will return one of the constants:
586 Type::Tiny::CMP_SUBTYPE (first type is a subtype of second type),
587 Type::Tiny::CMP_SUPERTYPE (second type is a subtype of first type),
588 Type::Tiny::CMP_EQUAL (the two types are exactly the same),
589 Type::Tiny::CMP_EQUIVALENT (the two types are effectively the
590 same), or Type::Tiny::CMP_UNKNOWN (couldn't establish any
591 relationship). In numeric contexts, these evaluate to -1, 1, 0, 0,
592 and 0, making it potentially usable with "sort" (though you may
593 need to silence warnings about treating the empty string as a
594 numeric value).
595
596 List processing methods
597
598 grep(@list)
599 Filters a list to return just the items that pass the type check.
600
601 @integers = Int->grep(@list);
602
603 first(@list)
604 Filters the list to return the first item on the list that passes
605 the type check, or undef if none do.
606
607 $first_lady = Woman->first(@people);
608
609 map(@list)
610 Coerces a list of items. Only works on types which have a coercion.
611
612 @truths = Bool->map(@list);
613
614 sort(@list)
615 Sorts a list of items according to the type's preferred sorting
616 mechanism, or if the type doesn't have a sorter coderef, uses the
617 parent type. If no ancestor type constraint has a sorter, throws an
618 exception. The "Str", "StrictNum", "LaxNum", and "Enum" type
619 constraints include sorters.
620
621 @sorted_numbers = Num->sort( Num->grep(@list) );
622
623 rsort(@list)
624 Like "sort" but backwards.
625
626 any(@list)
627 Returns true if any of the list match the type.
628
629 if ( Int->any(@numbers) ) {
630 say "there was at least one integer";
631 }
632
633 all(@list)
634 Returns true if all of the list match the type.
635
636 if ( Int->all(@numbers) ) {
637 say "they were all integers";
638 }
639
640 assert_any(@list)
641 Like "any" but instead of returning a boolean, returns the entire
642 original list if any item on it matches the type, and dies if none
643 does.
644
645 assert_all(@list)
646 Like "all" but instead of returning a boolean, returns the original
647 list if all items on it match the type, but dies as soon as it
648 finds one that does not.
649
650 Inlining methods
651
652 The following methods are used to generate strings of Perl code which
653 may be pasted into stringy "eval"uated subs to perform type checks:
654
655 "can_be_inlined"
656 Returns boolean indicating if this type can be inlined.
657
658 inline_check($varname)
659 Creates a type constraint check for a particular variable as a
660 string of Perl code. For example:
661
662 print( Types::Standard::Num->inline_check('$foo') );
663
664 prints the following output:
665
666 (!ref($foo) && Scalar::Util::looks_like_number($foo))
667
668 For Moose-compat, there is an alias "_inline_check" for this
669 method.
670
671 inline_assert($varname)
672 Much like "inline_check" but outputs a statement of the form:
673
674 ... or die ...;
675
676 Can also be called line "inline_assert($varname, $typevarname,
677 %extras)". In this case, it will generate a string of code that
678 may include $typevarname which is supposed to be the name of a
679 variable holding the type itself. (This is kinda complicated, but
680 it allows a useful string to still be produced if the type is not
681 inlineable.) The %extras are additional options to be passed to
682 Error::TypeTiny::Assertion's constructor and must be key-value
683 pairs of strings only, no references or undefs.
684
685 Other methods
686
687 "qualified_name"
688 For non-anonymous type constraints that have a library, returns a
689 qualified "MyLib::MyType" sort of name. Otherwise, returns the same
690 as "name".
691
692 isa($class), can($method), AUTOLOAD(@args)
693 If Moose is loaded, then the combination of these methods is used
694 to mock a Moose::Meta::TypeConstraint.
695
696 If Mouse is loaded, then "isa" mocks Mouse::Meta::TypeConstraint.
697
698 DOES($role)
699 Overridden to advertise support for various roles.
700
701 See also Type::API::Constraint, etc.
702
703 "TIESCALAR", "TIEARRAY", "TIEHASH"
704 These are provided as hooks that wrap Type::Tie. They allow the
705 following to work:
706
707 use Types::Standard qw(Int);
708 tie my @list, Int;
709 push @list, 123, 456; # ok
710 push @list, "Hello"; # dies
711
712 exportables( $base_name )
713 Returns a list of the functions a type library should export if it
714 contains this type constraint.
715
716 Example:
717
718 [
719 { name => 'Int', tags => [ 'types' ], code => sub { ... } },
720 { name => 'is_Int', tags => [ 'is' ], code => sub { ... } },
721 { name => 'assert_Int', tags => [ 'assert' ], code => sub { ... } },
722 { name => 'to_Int', tags => [ 'to' ], code => sub { ... } },
723 ]
724
725 $base_name is optional, but allows you to get a list of exportables
726 using a specific name. This is useful if the type constraint has a
727 name which wouldn't be a legal Perl function name.
728
729 "exportables_by_tag( $tag, $base_name )"
730 Filters "exportables" by a specific tag name. In list context,
731 returns all matching exportables. In scalar context returns a
732 single matching exportable and dies if multiple exportables match,
733 or none do!
734
735 The following methods exist for Moose/Mouse compatibility, but do not
736 do anything useful.
737
738 "compile_type_constraint"
739 "hand_optimized_type_constraint"
740 "has_hand_optimized_type_constraint"
741 "inline_environment"
742 "meta"
743
744 Overloading
745 • Stringification is overloaded to return the qualified name.
746
747 • Boolification is overloaded to always return true.
748
749 • Coderefification is overloaded to call "assert_return".
750
751 • On Perl 5.10.1 and above, smart match is overloaded to call
752 "check".
753
754 • The "==" operator is overloaded to call "equals".
755
756 • The "<" and ">" operators are overloaded to call "is_subtype_of"
757 and "is_supertype_of".
758
759 • The "~" operator is overloaded to call "complementary_type".
760
761 • The "|" operator is overloaded to build a union of two type
762 constraints. See Type::Tiny::Union.
763
764 • The "&" operator is overloaded to build the intersection of two
765 type constraints. See Type::Tiny::Intersection.
766
767 • The "/" operator provides magical Devel::StrictMode support. If
768 $ENV{PERL_STRICT} (or a few other environment variables) is true,
769 then it returns the left operand. Normally it returns the right
770 operand.
771
772 Previous versions of Type::Tiny would overload the "+" operator to call
773 "plus_coercions" or "plus_fallback_coercions" as appropriate. Support
774 for this was dropped after 0.040.
775
776 Constants
777 "Type::Tiny::SUPPORT_SMARTMATCH"
778 Indicates whether the smart match overload is supported on your
779 version of Perl.
780
781 Package Variables
782 $Type::Tiny::DD
783 This undef by default but may be set to a coderef that Type::Tiny
784 and related modules will use to dump data structures in things like
785 error messages.
786
787 Otherwise Type::Tiny uses it's own routine to dump data structures.
788 $DD may then be set to a number to limit the lengths of the dumps.
789 (Default limit is 72.)
790
791 This is a package variable (rather than get/set class methods) to
792 allow for easy localization.
793
794 $Type::Tiny::AvoidCallbacks
795 If this variable is set to true (you should usually do it in a
796 "local" scope), it acts as a hint for type constraints, when
797 generating inlined code, to avoid making any callbacks to variables
798 and functions defined outside the inlined code itself.
799
800 This should have the effect that "$type->inline_check('$foo')" will
801 return a string of code capable of checking the type on Perl
802 installations that don't have Type::Tiny installed. This is
803 intended to allow Type::Tiny to be used with things like Mite.
804
805 The variable works on the honour system. Types need to explicitly
806 check it and decide to generate different code based on its truth
807 value. The bundled types in Types::Standard,
808 Types::Common::Numeric, and Types::Common::String all do.
809 (StrMatch is sometimes unable to, and will issue a warning if it
810 needs to rely on callbacks when asked not to.)
811
812 Most normal users can ignore this.
813
814 $Type::Tiny::SafePackage
815 This is the string "package Type::Tiny;" which is sometimes
816 inserted into strings of inlined code to avoid namespace clashes.
817 In most cases, you do not need to change this. However, if you are
818 inlining type constraint code, saving that code into Perl modules,
819 and uploading them to CPAN, you may wish to change it to avoid
820 problems with the CPAN indexer. Most normal users of Type::Tiny do
821 not need to be aware of this.
822
823 Environment
824 "PERL_TYPE_TINY_XS"
825 Currently this has more effect on Types::Standard than Type::Tiny.
826 In future it may be used to trigger or suppress the loading XS
827 implementations of parts of Type::Tiny.
828
830 Please report any bugs to
831 <https://github.com/tobyink/p5-type-tiny/issues>.
832
834 The Type::Tiny homepage <https://typetiny.toby.ink/>.
835
836 Type::Tiny::Manual, Type::API.
837
838 Type::Library, Type::Utils, Types::Standard, Type::Coercion.
839
840 Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck,
841 Type::Tiny::Enum, Type::Tiny::Union, Type::Tiny::Intersection.
842
843 Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint.
844
845 Type::Params.
846
847 Type::Tiny on GitHub <https://github.com/tobyink/p5-type-tiny>,
848 Type::Tiny on Travis-CI <https://travis-ci.com/tobyink/p5-type-tiny>,
849 Type::Tiny on AppVeyor
850 <https://ci.appveyor.com/project/tobyink/p5-type-tiny>, Type::Tiny on
851 Codecov <https://codecov.io/gh/tobyink/p5-type-tiny>, Type::Tiny on
852 Coveralls <https://coveralls.io/github/tobyink/p5-type-tiny>.
853
855 Toby Inkster <tobyink@cpan.org>.
856
858 Thanks to Matt S Trout for advice on Moo integration.
859
861 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
862
863 This is free software; you can redistribute it and/or modify it under
864 the same terms as the Perl 5 programming language system itself.
865
867 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
868 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
869 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
870
871
872
873perl v5.38.0 2023-07-21 Type::Tiny(3)