line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::SingleArg; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$MooseX::SingleArg::VERSION = '0.09'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
MooseX::SingleArg - No-fuss instantiation of Moose objects using a single argument. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package Person; |
12
|
|
|
|
|
|
|
use Moose; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use MooseX::SingleArg; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
single_arg 'name'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has name => ( is=>'ro', isa=>'Str' ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $john = Person->new( 'John Doe' ); |
21
|
|
|
|
|
|
|
print $john->name(); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
This module allows Moose instances to be constructed with a single argument. |
26
|
|
|
|
|
|
|
Your class or role must use this module and then use the single_arg sugar to |
27
|
|
|
|
|
|
|
declare which attribute will be assigned the single argument value. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
If the class is constructed using the typical argument list name/value pairs, |
30
|
|
|
|
|
|
|
or with a hashref, then things work as is usual. But, if the arguments are a |
31
|
|
|
|
|
|
|
single non-hashref value then that argument will be assigned to whatever |
32
|
|
|
|
|
|
|
attribute you have declared. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The reason for this module's existence is that when people want this feature |
35
|
|
|
|
|
|
|
they usually find L<Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD> which |
36
|
|
|
|
|
|
|
asks that something like the following be written: |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
around BUILDARGS => sub { |
39
|
|
|
|
|
|
|
my $orig = shift; |
40
|
|
|
|
|
|
|
my $class = shift; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
if ( @_ == 1 && ! ref $_[0] ) { |
43
|
|
|
|
|
|
|
return $class->$orig(ssn => $_[0]); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
|
|
|
|
|
|
return $class->$orig(@_); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The above is complex boilerplate for a simple feature. This module aims to make |
51
|
|
|
|
|
|
|
it simple and fool-proof to support single-argument Moose object construction. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 INIT_ARG BEHAVIOR |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
If setting a custom init_arg for an attribute which you will be assigning as the |
56
|
|
|
|
|
|
|
single_arg then use the init_arg value, rather than the attribute key, for it. |
57
|
|
|
|
|
|
|
For example: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
single_arg 'moniker'; |
60
|
|
|
|
|
|
|
has name => ( is=>'ro', isa=>'Str', init_arg=>'moniker' ); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 FORCING SINGLE ARG PROCESSING |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
An optional force parameter may be specified: |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
single_arg name => ( |
67
|
|
|
|
|
|
|
force => 1, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
This causes constructor argument processing to only work in single-argument mode. If |
71
|
|
|
|
|
|
|
more than one argument is passed then an error will be thrown. The benefit of forcing |
72
|
|
|
|
|
|
|
single argument processing is that hashrefs may now be used as the value of the single |
73
|
|
|
|
|
|
|
argument when force is on. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
2
|
|
1128357
|
use Moose (); |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
43
|
|
78
|
2
|
|
|
2
|
|
8
|
use Moose::Exporter; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13
|
|
79
|
2
|
|
|
2
|
|
74
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
382
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
82
|
|
|
|
|
|
|
with_meta => ['single_arg'], |
83
|
|
|
|
|
|
|
class_metaroles => { |
84
|
|
|
|
|
|
|
class => ['MooseX::SingleArg::Meta::Class'], |
85
|
|
|
|
|
|
|
}, |
86
|
|
|
|
|
|
|
role_metaroles => { |
87
|
|
|
|
|
|
|
role => ['MooseX::SingleArg::Meta::Role'], |
88
|
|
|
|
|
|
|
application_to_class => ['MooseX::SingleArg::Meta::ToClass'], |
89
|
|
|
|
|
|
|
application_to_role => ['MooseX::SingleArg::Meta::ToRole'], |
90
|
|
|
|
|
|
|
}, |
91
|
|
|
|
|
|
|
base_class_roles => ['MooseX::SingleArg::Meta::Object'], |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub single_arg { |
95
|
5
|
|
|
5
|
0
|
2632
|
my ($meta, $name, %args) = @_; |
96
|
|
|
|
|
|
|
|
97
|
5
|
|
|
|
|
63
|
my $class = $meta->name(); |
98
|
5
|
100
|
|
|
|
198
|
croak "A single arg has already been declared for $class" if $meta->has_single_arg(); |
99
|
|
|
|
|
|
|
|
100
|
4
|
|
|
|
|
117
|
$meta->single_arg( $name ); |
101
|
|
|
|
|
|
|
|
102
|
4
|
|
|
|
|
12
|
foreach my $arg (keys %args) { |
103
|
1
|
|
|
|
|
4
|
my $method = $arg . '_single_arg'; |
104
|
1
|
50
|
|
|
|
12
|
croak("Unknown single_arg argument $arg") if !$meta->can($method); |
105
|
1
|
|
|
|
|
32
|
$meta->$method( $args{$arg} ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
4
|
|
|
|
|
12
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
__END__ |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SEE ALSO |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
L<MooseX::OneArgNew> solves the same problem that this module solves. I considered using OneArgNew |
117
|
|
|
|
|
|
|
for my own needs, but found it oddly cumbersome and confusing. Maybe that's just me, but I hope that |
118
|
|
|
|
|
|
|
this module's design is much simpler to comprehend and more natural to use. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHOR |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Aran Clary Deltac <bluefeet@gmail.com> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Xavier Guimard <x.guimardE<64>free.fr> |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Mohammad S Anwar <mohammad.anwarE<64>yahoo.com> |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=back |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 LICENSE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
141
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
142
|
|
|
|
|
|
|
|