1MooseX::OneArgNew(3) User Contributed Perl Documentation MooseX::OneArgNew(3)
2
3
4
6 MooseX::OneArgNew - teach ->new to accept single, non-hashref arguments
7
9 version 0.007
10
12 In our class definition:
13
14 package Delivery;
15 use Moose;
16 with('MooseX::OneArgNew' => {
17 type => 'Existing::Message::Type',
18 init_arg => 'message',
19 });
20
21 has message => (isa => 'Existing::Message::Type', required => 1);
22
23 has to => (
24 is => 'ro',
25 isa => 'Str',
26 lazy => 1,
27 default => sub {
28 my ($self) = @_;
29 $self->message->get('To');
30 },
31 );
32
33 When making a message:
34
35 # The traditional way:
36
37 my $delivery = Delivery->new({ message => $message });
38 # or
39 my $delivery = Delivery->new({ message => $message, to => $to });
40
41 # With one-arg new:
42
43 my $delivery = Delivery->new($message);
44
46 MooseX::OneArgNew lets your constructor take a single argument, which
47 will be translated into the value for a one-entry hashref. It is a
48 parameterized role with three parameters:
49
50 type
51 The Moose type that the single argument must be for the one-arg
52 form to work. This should be an existing type, and may be either a
53 string type or a MooseX::Type.
54
55 init_arg
56 This is the string that will be used as the key for the hashref
57 constructed from the one-arg call to new.
58
59 coerce
60 If true, a single argument to new will be coerced into the expected
61 type if possible. Keep in mind that if there are no coercions for
62 the type, this will be an error, and that if a coercion from
63 HashRef exists, you might be getting yourself into a weird
64 situation.
65
66 WARNINGS
67 You can apply MooseX::OneArgNew more than once, but if more than one
68 application's type matches a single argument to "new", the behavior is
69 undefined and likely to cause bugs.
70
71 It would be a very bad idea to supply a type that could accept a normal
72 hashref of arguments to "new".
73
75 This module should work on any version of perl still receiving updates
76 from the Perl 5 Porters. This means it should work on any version of
77 perl released in the last two to three years. (That is, if the most
78 recently released version is v5.40, then this module should work on
79 both v5.40 and v5.38.)
80
81 Although it may work on older versions of perl, no guarantee is made
82 that the minimum required version will not be increased. The version
83 may be increased for any reason, and there is no promise that patches
84 will be accepted to lower the minimum required perl.
85
87 Ricardo Signes <cpan@semiotic.systems>
88
90 • George Hartzell <hartzell@alerce.com>
91
92 • Ricardo Signes <rjbs@semiotic.systems>
93
94 • William Orr <will@worrbase.com>
95
97 This software is copyright (c) 2022 by Ricardo Signes.
98
99 This is free software; you can redistribute it and/or modify it under
100 the same terms as the Perl 5 programming language system itself.
101
102
103
104perl v5.36.0 2023-01-20 MooseX::OneArgNew(3)