1MouseX::Types(3) User Contributed Perl Documentation MouseX::Types(3)
2
3
4
6 MouseX::Types - Organize your Mouse types in libraries
7
9 Library Definition
10 package MyLibrary;
11
12 # predeclare our own types
13 use MouseX::Types
14 -declare => [qw(
15 PositiveInt NegativeInt
16 )];
17
18 # import builtin types
19 use MouseX::Types::Mouse 'Int';
20
21 # type definition.
22 subtype PositiveInt,
23 as Int,
24 where { $_ > 0 },
25 message { "Int is not larger than 0" };
26
27 subtype NegativeInt,
28 as Int,
29 where { $_ < 0 },
30 message { "Int is not smaller than 0" };
31
32 # type coercion
33 coerce PositiveInt,
34 from Int,
35 via { 1 };
36
37 1;
38
39 Usage
40 package Foo;
41 use Mouse;
42 use MyLibrary qw( PositiveInt NegativeInt );
43
44 # use the exported constants as type names
45 has 'bar',
46 isa => PositiveInt,
47 is => 'rw';
48 has 'baz',
49 isa => NegativeInt,
50 is => 'rw';
51
52 sub quux {
53 my ($self, $value);
54
55 # test the value
56 print "positive\n" if is_PositiveInt($value);
57 print "negative\n" if is_NegativeInt($value);
58
59 # coerce the value, NegativeInt doesn't have a coercion
60 # helper, since it didn't define any coercions.
61 $value = to_PositiveInt($value) or die "Cannot coerce";
62 }
63
64 1;
65
67 Kazuhiro Osawa <yappo <at> shibuya <döt> pl>
68
69 Shawn M Moore
70
71 tokuhirom
72
73 Goro Fuji
74
75 with plenty of code borrowed from MooseX::Types
76
78 git clone git://github.com/yappo/p5-mousex-types.git MouseX-Types
79
81 Mouse
82
83 MooseX::Types
84
86 Copyright (c) 2008-2010, Kazuhiro Osawa and partly based on
87 MooseX::Types, which is (c) Robert Sedlacek.
88
89 This library is free software; you can redistribute it and/or modify it
90 under the same terms as Perl itself.
91
92
93
94perl v5.32.1 2021-01-27 MouseX::Types(3)