Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 76 lines (49 sloc) 2.357 kb
cbbd773 @bluefeet README.
authored
1 =head1 NAME
2
3 MooseX::SingleArg - No-fuss instantiation of Moose objects using a single argument.
4
5 =head1 SYNOPSIS
6
7 package Person;
8 use Moose;
9
10 use MooseX::SingleArg;
11
12 single_arg 'name';
13
14 has name => ( is=>'ro', isa=>'Str' );
31862be @bluefeet - Comment on MooseX::OneArgNew.
authored
15
cbbd773 @bluefeet README.
authored
16 my $john = Person->new( 'John Doe' );
17 print $john->name();
18
19 =head1 DESCRIPTION
20
5626557 @bluefeet Use Moose's meta traits more effectively so that single args may be d…
authored
21 This module allows Moose instances to be constructed with a single argument.
dd2499a @bluefeet Minor doc updates.
authored
22 Your class or role must use this module and then use the single_arg sugar to
5626557 @bluefeet Use Moose's meta traits more effectively so that single args may be d…
authored
23 declare which attribute will be assigned the single argument value.
cbbd773 @bluefeet README.
authored
24
31862be @bluefeet - Comment on MooseX::OneArgNew.
authored
25 If the class is constructed using the typical argument list name/value pairs,
cbbd773 @bluefeet README.
authored
26 or with a hashref, then things work as is usual. But, if the arguments are a
27 single non-hashref value then that argument will be assigned to whatever
28 attribute you have declared.
29
30 The reason for this module's existence is that when people want this feature
31 they usually find L<Moose::Cookbook::Basics::Recipe10> which asks that something
32 like the following be written:
33
34 around BUILDARGS => sub{
35 my $orig = shift;
36 my $self = shift;
37
38 if (@_==1 and ref($_[0]) ne 'HASH') {
39 return $self->$orig( foo => $_[0] );
40 }
41
42 return $self->$orig( @_ );
43 };
44
45 The above is complex boilerplate for a simple feature. This module aims to make
46 it simple and fool-proof to support single-argument Moose object construction.
47
31862be @bluefeet - Comment on MooseX::OneArgNew.
authored
48 =head1 FORCING SINGLE ARG PROCESSING
49
50 An optional force parameter may be specified:
51
52 single_arg name => (
53 force => 1,
54 );
55
56 This causes constructor argument processing to only work in single-argument mode. If
57 more than one argument is passed then an error will be thrown. The benefit of forcing
58 single argument processing is that hashrefs may now be used as the value of the single
59 argument when force is on.
60
61 =head1 SEE ALSO
62
63 L<MooseX::OneArgNew> solves the same problem that this module solves. I considered using OneArgNew
64 for my own needs, but found it oddly combersom and confusing. Maybe thats just me, but I hope that
65 this module's design is much simpler to comprehend and more natural to use.
66
cbbd773 @bluefeet README.
authored
67 =head1 AUTHOR
68
69 Aran Clary Deltac <bluefeet@gmail.com>
70
71 =head1 LICENSE
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
Something went wrong with that request. Please try again.