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.002
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 two 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 WARNINGS
60 You can apply MooseX::OneArgNew more than once, but if more than one
61 application's type matches a single argument to "new", the behavior is
62 undefined and likely to cause bugs.
63
64 It would be a very bad idea to supply a type that could accept a normal
65 hashref of arguments to "new".
66
68 Ricardo Signes <rjbs@cpan.org>
69
71 This software is copyright (c) 2011 by Ricardo Signes.
72
73 This is free software; you can redistribute it and/or modify it under
74 the same terms as the Perl 5 programming language system itself.
75
76
77
78perl v5.12.3 2011-06-15 MooseX::OneArgNew(3)