1MooseX::OneArgNew(3)  User Contributed Perl Documentation MooseX::OneArgNew(3)
2
3
4

NAME

6       MooseX::OneArgNew - teach ->new to accept single, non-hashref arguments
7

VERSION

9       version 0.005
10

SYNOPSIS

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

DESCRIPTION

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

AUTHOR

75       Ricardo Signes <rjbs@cpan.org>
76

CONTRIBUTORS

78       ·   George Hartzell <hartzell@alerce.com>
79
80       ·   William Orr <will@worrbase.com>
81
83       This software is copyright (c) 2015 by Ricardo Signes.
84
85       This is free software; you can redistribute it and/or modify it under
86       the same terms as the Perl 5 programming language system itself.
87
88
89
90perl v5.30.1                      2020-01-30              MooseX::OneArgNew(3)
Impressum