1MooseX::Has::Options(3)User Contributed Perl DocumentatioMnooseX::Has::Options(3)
2
3
4
6 MooseX::Has::Options - Succinct options for Moose
7
9 version 0.003
10
12 use Moose;
13 use MooseX::Has::Options;
14
15 has 'some_attribute' => (
16 qw(:ro :required),
17 isa => 'Str',
18 ...
19 );
20
21 has 'another_attribute' => (
22 qw(:ro :lazy_build),
23 isa => 'Str',
24 ...
25 );
26
28 This module provides a succinct syntax for declaring options for Moose
29 attributes.
30
32 Declaring options
33 "MooseX::Has::Params" works by checking the arguments to "has" for
34 strings that look like options, i.e. alphanumeric strings preceded by a
35 colon, and replaces them with a hash whose keys are the names of the
36 options (sans the colon) and the values are 1's. Thus,
37
38 has 'some_attribute', ':required';
39
40 becomes:
41
42 has 'some_attribute', required => 1;
43
44 Options must come in the beginning of the argument list.
45 MooseX::Has::Options will stop searching for options after the first
46 alphanumeric string that does not start with a colon.
47
48 The default behaviour can be customised per attribute. For example,
49 here is how "ro", "rw" and "bare" work:
50
51 has 'some_attribute', ':ro';
52
53 becomes:
54
55 has 'some_attribute', is => 'ro';
56
57 See below for details.
58
59 Handlers
60 "MooseX::Has::Options" allows you to expand specific 'shortcut'
61 arguments to arbitrary values via the handler interface. A 'handler' is
62 a module in the MooseX::Has::Options::Handler namespace that provides a
63 "handler" function. The handler function should return a hash whose
64 keys are shortcut names, and the values are hashrefs with the values
65 that the respective shortcuts should be expanded to. In order to enable
66 the shortcuts supplied by a given handler you need to add it in the
67 import statement:
68
69 use MooseX::Has::Options qw(NativeTypes);
70
71 has 'some_attribute', qw(:ro :hash), default => sub {{ foo => bar }};
72
73 The following handlers ship with the default distribution:
74
75 • MooseX::Has::Options::Handler::Accessors (included by default when
76 you import this module)
77
78 • MooseX::Has::Options::Handler::NativeTypes
79
80 • MooseX::Has::Options::Handler::NoInit
81
83 "MooseX::Has::Options" hijacks the "has" function imported by Moose and
84 replaces it with one that understands the options syntax described
85 above. This is not an optimal solution, but the current implementation
86 of "Moose::Meta::Attribute" prevents this functionality from being
87 provided as a meta trait.
88
90 Previous versions of "MooseX::Has::Params" allowed you to specify
91 during import the name of the function too hook into, like so:
92
93 use HTML::FormHandler::Moose;
94 use MooseX::Has::Options qw(has_field);
95
96 has_field 'name' => (
97 qw(:required),
98 type => 'Text',
99 );
100
101 This behaviour is deprecated as of version 0.003 as this syntax is now
102 used for specifying handlers. If you need to hook into a different
103 function see the implementation of MooseX::Has::Options::import() and
104 MooseX::Has::Options::import_into().
105
107 • MooseX::Has::Sugar
108
110 Peter Shangov <pshangov@yahoo.com>
111
113 This software is copyright (c) 2012 by Peter Shangov.
114
115 This is free software; you can redistribute it and/or modify it under
116 the same terms as the Perl 5 programming language system itself.
117
118
119
120perl v5.36.0 2023-01-20 MooseX::Has::Options(3)