MooseX::SingleArg3pm
No-fuss instantiation of moose objects using a single argument.
Usage
package Person;use Moose;use MooseX::SingleArg;single_arg 'name';has name => ( is=>'ro', isa=>'Str' );my $john = Person->new( 'John Doe' );print $john->name();
Overview
This module allows Moose instances to be constructed with a single argument. Your class or role must use this module and then use the single_arg sugar to declare which attribute will be assigned the single argument value.
If the class is constructed using the typical argument list name/value pairs, or with a hashref, then things work as is usual. But, if the arguments are a single non-hashref value then that argument will be assigned to whatever attribute you have declared.
The reason for this module's existence is that when people want this feature they usually find Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD which asks that something like the following be written:
around BUILDARGS => sub {my $orig = shift;my $class = shift;if ( @_ == 1 && ! ref $_[0] ) {return $class->$orig(ssn => $_[0]);}else {return $class->$orig(@_);}};
The above is complex boilerplate for a simple feature. This module aims to make it simple and fool-proof to support single-argument Moose object construction.
Init_arg behavior
If setting a custom init_arg for an attribute which you will be assigning as the single_arg then use the init_arg value, rather than the attribute key, for it. For example:
single_arg 'moniker';has name => ( is=>'ro', isa=>'Str', init_arg=>'moniker' );
Forcing single arg processing
An optional force parameter may be specified:
single_arg name => (force => 1,);
This causes constructor argument processing to only work in single-argument mode. If more than one argument is passed then an error will be thrown. The benefit of forcing single argument processing is that hashrefs may now be used as the value of the single argument when force is on.
Contributors
•
|
Xavier Guimard <[email protected]> |
|---|---|
•
|
Mohammad S Anwar <[email protected]> |
Releases
4 releases since Debian 10 (Buster); the text changed 1 time.