/usr/share/perl5/MooseX/OneArgNew.pm is in libmoosex-oneargnew-perl 0.005-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | package MooseX::OneArgNew;
$MooseX::OneArgNew::VERSION = '0.005';
use MooseX::Role::Parameterized 1.01;
# ABSTRACT: teach ->new to accept single, non-hashref arguments
#pod =head1 SYNOPSIS
#pod
#pod In our class definition:
#pod
#pod package Delivery;
#pod use Moose;
#pod with('MooseX::OneArgNew' => {
#pod type => 'Existing::Message::Type',
#pod init_arg => 'message',
#pod });
#pod
#pod has message => (isa => 'Existing::Message::Type', required => 1);
#pod
#pod has to => (
#pod is => 'ro',
#pod isa => 'Str',
#pod lazy => 1,
#pod default => sub {
#pod my ($self) = @_;
#pod $self->message->get('To');
#pod },
#pod );
#pod
#pod When making a message:
#pod
#pod # The traditional way:
#pod
#pod my $delivery = Delivery->new({ message => $message });
#pod # or
#pod my $delivery = Delivery->new({ message => $message, to => $to });
#pod
#pod # With one-arg new:
#pod
#pod my $delivery = Delivery->new($message);
#pod
#pod =head1 DESCRIPTION
#pod
#pod MooseX::OneArgNew lets your constructor take a single argument, which will be
#pod translated into the value for a one-entry hashref. It is a L<parameterized
#pod role|MooseX::Role::Parameterized> with three parameters:
#pod
#pod =begin :list
#pod
#pod = type
#pod
#pod The Moose type that the single argument must be for the one-arg form to work.
#pod This should be an existing type, and may be either a string type or a
#pod MooseX::Type.
#pod
#pod = init_arg
#pod
#pod This is the string that will be used as the key for the hashref constructed
#pod from the one-arg call to new.
#pod
#pod = coerce
#pod
#pod If true, a single argument to new will be coerced into the expected type if
#pod possible. Keep in mind that if there are no coercions for the type, this will
#pod be an error, and that if a coercion from HashRef exists, you might be getting
#pod yourself into a weird situation.
#pod
#pod =end :list
#pod
#pod =head2 WARNINGS
#pod
#pod You can apply MooseX::OneArgNew more than once, but if more than one
#pod application's type matches a single argument to C<new>, the behavior is
#pod undefined and likely to cause bugs.
#pod
#pod It would be a B<very bad idea> to supply a type that could accept a normal
#pod hashref of arguments to C<new>.
#pod
#pod =cut
use Moose::Util::TypeConstraints;
use namespace::autoclean;
subtype 'MooseX::OneArgNew::_Type',
as 'Moose::Meta::TypeConstraint';
coerce 'MooseX::OneArgNew::_Type',
from 'Str',
via { Moose::Util::TypeConstraints::find_type_constraint($_) };
parameter type => (
isa => 'MooseX::OneArgNew::_Type',
coerce => 1,
required => 1,
);
parameter coerce => (
isa => 'Bool',
default => 0,
);
parameter init_arg => (
isa => 'Str',
required => 1,
);
role {
my $p = shift;
around BUILDARGS => sub {
my $orig = shift;
my $self = shift;
return $self->$orig(@_) unless @_ == 1;
my $value = $p->coerce ? $p->type->coerce($_[0]) : $_[0];
return $self->$orig(@_) unless $p->type->check($value);
return { $p->init_arg => $value }
};
};
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::OneArgNew - teach ->new to accept single, non-hashref arguments
=head1 VERSION
version 0.005
=head1 SYNOPSIS
In our class definition:
package Delivery;
use Moose;
with('MooseX::OneArgNew' => {
type => 'Existing::Message::Type',
init_arg => 'message',
});
has message => (isa => 'Existing::Message::Type', required => 1);
has to => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => sub {
my ($self) = @_;
$self->message->get('To');
},
);
When making a message:
# The traditional way:
my $delivery = Delivery->new({ message => $message });
# or
my $delivery = Delivery->new({ message => $message, to => $to });
# With one-arg new:
my $delivery = Delivery->new($message);
=head1 DESCRIPTION
MooseX::OneArgNew lets your constructor take a single argument, which will be
translated into the value for a one-entry hashref. It is a L<parameterized
role|MooseX::Role::Parameterized> with three parameters:
=over 4
=item type
The Moose type that the single argument must be for the one-arg form to work.
This should be an existing type, and may be either a string type or a
MooseX::Type.
=item init_arg
This is the string that will be used as the key for the hashref constructed
from the one-arg call to new.
=item coerce
If true, a single argument to new will be coerced into the expected type if
possible. Keep in mind that if there are no coercions for the type, this will
be an error, and that if a coercion from HashRef exists, you might be getting
yourself into a weird situation.
=back
=head2 WARNINGS
You can apply MooseX::OneArgNew more than once, but if more than one
application's type matches a single argument to C<new>, the behavior is
undefined and likely to cause bugs.
It would be a B<very bad idea> to supply a type that could accept a normal
hashref of arguments to C<new>.
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
=head1 CONTRIBUTORS
=for stopwords George Hartzell William Orr
=over 4
=item *
George Hartzell <hartzell@alerce.com>
=item *
William Orr <will@worrbase.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|