1Type::Tiny::Duck(3) User Contributed Perl Documentation Type::Tiny::Duck(3)
2
3
4
6 Type::Tiny::Duck - type constraints based on the "can" method
7
9 Using via Types::Standard:
10
11 package Logger {
12 use Moo;
13 use Types::Standard qw( HasMethods Bool );
14
15 has debugging => ( is => 'rw', isa => Bool, default => 0 );
16 has output => ( is => 'ro', isa => HasMethods[ 'print' ] );
17
18 sub warn {
19 my ( $self, $message ) = @_;
20 $self->output->print( "[WARNING] $message\n" );
21 }
22
23 sub debug {
24 my ( $self, $message ) = @_;
25 $self->output->print( "[DEBUG] $message\n" ) if $self->debugging;
26 }
27 }
28
29 Using Type::Tiny::Duck's export feature:
30
31 package Logger {
32 use Moo;
33 use Types::Standard qw( Bool );
34 use Type::Tiny::Duck Printable => [ 'print' ];
35
36 has debugging => ( is => 'rw', isa => Bool, default => 0 );
37 has output => ( is => 'ro', isa => Printable );
38
39 sub warn {
40 my ( $self, $message ) = @_;
41 $self->output->print( "[WARNING] $message\n" );
42 }
43
44 sub debug {
45 my ( $self, $message ) = @_;
46 $self->output->print( "[DEBUG] $message\n" ) if $self->debugging;
47 }
48 }
49
50 Using Type::Tiny::Duck's object-oriented interface:
51
52 package Logger {
53 use Moo;
54 use Types::Standard qw( Bool );
55 use Type::Tiny::Duck;
56
57 my $Printable = Type::Type::Duck->new(
58 name => 'Printable',
59 methods => [ 'print' ],
60 );
61
62 has debugging => ( is => 'rw', isa => Bool, default => 0 );
63 has output => ( is => 'ro', isa => $Printable );
64
65 sub warn {
66 my ( $self, $message ) = @_;
67 $self->output->print( "[WARNING] $message\n" );
68 }
69
70 sub debug {
71 my ( $self, $message ) = @_;
72 $self->output->print( "[DEBUG] $message\n" ) if $self->debugging;
73 }
74 }
75
77 This module is covered by the Type-Tiny stability policy.
78
80 Type constraints of the general form "{ $_->can("method") }".
81
82 The name refers to the saying, "If it looks like a duck, swims like a
83 duck, and quacks like a duck, then it probably is a duck". Duck typing
84 can be a more flexible way of testing objects than relying on "isa", as
85 it allows people to easily substitute mock objects.
86
87 This package inherits from Type::Tiny; see that for most documentation.
88 Major differences are listed below:
89
90 Attributes
91 "methods"
92 An arrayref of method names.
93
94 "constraint"
95 Unlike Type::Tiny, you cannot pass a constraint coderef to the
96 constructor. Instead rely on the default.
97
98 "inlined"
99 Unlike Type::Tiny, you cannot pass an inlining coderef to the
100 constructor. Instead rely on the default.
101
102 "parent"
103 Parent is always Types::Standard::Object, and cannot be passed to
104 the constructor.
105
106 Methods
107 stringifies_to($constraint)
108 See Type::Tiny::ConstrainedObject.
109
110 numifies_to($constraint)
111 See Type::Tiny::ConstrainedObject.
112
113 "with_attribute_values($attr1 => $constraint1, ...)"
114 See Type::Tiny::ConstrainedObject.
115
116 Exports
117 Type::Tiny::Duck can be used as an exporter.
118
119 use Type::Tiny::Duck HttpClient => [ 'get', 'post' ];
120
121 This will export the following functions into your namespace:
122
123 "HttpClient"
124 is_HttpClient( $value )
125 assert_HttpClient( $value )
126 to_HttpClient( $value )
127
128 Multiple types can be exported at once:
129
130 use Type::Tiny::Duck (
131 HttpClient => [ 'get', 'post' ],
132 FtpClient => [ 'upload', 'download' ],
133 );
134
136 Please report any bugs to
137 <https://github.com/tobyink/p5-type-tiny/issues>.
138
140 Type::Tiny::Manual.
141
142 Type::Tiny.
143
144 Moose::Meta::TypeConstraint::DuckType.
145
147 Toby Inkster <tobyink@cpan.org>.
148
150 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
151
152 This is free software; you can redistribute it and/or modify it under
153 the same terms as the Perl 5 programming language system itself.
154
156 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
157 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
158 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
159
160
161
162perl v5.36.0 2023-04-24 Type::Tiny::Duck(3)