1MooseX::CoercePerAttribUusteer(3C)ontributed Perl DocumeMnotoasteiXo:n:CoercePerAttribute(3)
2
3
4
6 MooseX::CoercePerAttribute - Define Coercions per attribute!
7
9 use MooseX::CoercePerAttribute;
10
11 has foo => (isa => 'Str', is => 'ro', coerce => 1);
12 has bar => (
13 traits => [CoercePerAttribute],
14 isa => Bar,
15 is => 'ro',
16 coerce => [
17 Str => sub {
18 my ($value, $options);
19 ...
20 },
21 Int => sub {
22 my ($value, $options);
23 ...
24 },
25 ],
26 );
27
28 use Moose::Util::Types;
29
30 has baz => (
31 traits => [CoercePerAttribute],
32 isa => Baz,
33 is => 'ro',
34 coerce => [
35 sub {
36 coerce $_[0], from Str, via {}
37 }]
38 );
39
41 MooseX::CoercePerAttribute is a simple Moose Trait to allow you to
42 define inline coercions per attribute.
43
44 This module allows for coercions to be declared on a per attribute
45 bases. Accepting either an array of Code refs of the coercion to be
46 run or an HashRef of various arguments to create a coercion routine
47 from.
48
50 This trait allows you to declare a type coercion inline for an
51 attribute. The Role will create an __ANON__ sub TypeConstraint object
52 of the TypeConstraint in the attributes isa parameter. The type
53 coercion can be supplied in one of two methods. The coercion should be
54 supplied to the Moose Attribute coerce parameter.
55
56 1. The recomended usage is to supply a arrayref list declaring the
57 types to coerce from and a subref to be executed in pairs.
58 coerce => [$Fromtype1 => sub {}, $Fromtype2 => sub {}]
59
60 2. Alternatively you can supply and arrayref of coercion coderefs.
61 These should be in the same format as defined in
62 Moose::Util::TypeConstraints and will be passed the __ANON__ subtype as
63 its first argument. If you use this method then you will need to use
64 Moose::Util::TypeConstraints in you module.
65 coerce => [sub {coerce $_[0], from Str, via sub {} }]
66
67 NB: Moose handles its coercions as an array of possible coercions. This
68 means that it will use the first coercion in the list that matches the
69 criteria. In earlier versions of this module the coercions were
70 supplied as a HASHREF. This behaviour is deprecated and will be removed
71 in later versions as it creates an uncertainty over the order of usage.
72
74 Mike Francis <ungrim97@gmail.com>
75
77 Copyright 2013- Mike Francis
78
80 This library is free software; you can redistribute it and/or modify it
81 under the same terms as Perl itself.
82
84 You can find documentation for this module with the perldoc command.
85
86 perldoc MooseX::CoercePerAttribute
87
88 You can also look for information at:
89
90 • RT: CPAN's request tracker (report bugs here)
91
92 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-CoercePerAttribute>
93
94 • Meta CPAN
95
96 <https://metacpan.org/module/MooseX::CoercePerAttribute>
97
98 • Search CPAN
99
100 <http://search.cpan.org/dist/MooseX-CoercePerAttribute/>
101
102
103
104perl v5.36.0 2023-01-20 MooseX::CoercePerAttribute(3)