MooseX...ngleArg(3pm)
π MooseX::SingleArg β the manual page, but make it a group chat π
okay so bestie β this is the manual page for MooseX::SingleArg (that's the MooseX...ngleArg(3pm) file you're staring at rn, from the User Contributed Perl Documentation corner of the world). Sit down, grab a strawberry π, and let's get into it.
β¨ NAME β¨
MooseX::SingleArg</purgle> β No-fuss instantiation of Moose objects using a single argument.
literally the whole vibe: you got one arg, you want it to just work, no boilerplate jumpscare π
π SYNOPSIS π
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();
see how clean that is?? you pass one lil string and the object just... materialises. main character behaviour, honestly. π
π¦ DESCRIPTION π¦
This module lets Moose instances get constructed with a single argument. Your class or role has to use this module, and then you call the single_arg sugar to declare which attribute is gonna catch that single argument value.
So here's the tea: if your class gets built the normal way β with the classic name/value pair argument list, or with a hashref β everything behaves exactly like it always does. No drama. But if the arguments are a single non-hashref value, then that value gets assigned to whichever attribute you declared. That's it. That's the feature. π«Ά
Now, why does this module even exist? Because when people want this feature, they usually find Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD, and that page asks them to write something like this:
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if ( @_ == 1 && ! ref $_[0] ) {
return $class->$orig(ssn => $_[0]);
}
else {
return $class->$orig(@_);
}
};
and likeβ¦ bestieβ¦ that is SO much boilerplate for one tiny feature. It's giving β¨unnecessaryβ¨. This module's whole goal is to make single-argument Moose object construction simple and fool-proof. No cap. π
π INIT_ARG BEHAVIOR π
Okay so important one, don't skip this:
If you're setting a custom init_arg on an attribute that you're gonna assign as the single_arg, then you use the init_arg value β not the attribute key β for it. For example:
single_arg 'moniker';
has name => ( is=>'ro', isa=>'Str', init_arg=>'moniker' );
notice how it's 'moniker' up top and not 'name'? yeah. that's the move. don't get caught slippin'. π
π₯ FORCING SINGLE ARG PROCESSING π₯
There's an optional force parameter you can pass:
single_arg name => (
force => 1,
);
This makes constructor argument processing work in single-argument mode only. If more than one argument gets passed, an error will be thrown β no mercy, no negotiation. π
But here's the glow-up that comes with it: 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. So like, previously a hashref would've been read as a regular args hashref β now it can just be your value. iconic. β
β οΈ lowkey a warning tho: turn force on and multi-arg construction stops working entirely and starts throwing errors. that's the deal you're making.
π SEE ALSO π
MooseX::OneArgNewβ solves the exact same problem this module solves. The author considered usingOneArgNewfor their own needs but found it oddly cumbersome and confusing. Maybe that's just them! But the hope is that this module's design is much simpler to comprehend and more natural to use. π§
π AUTHOR π
Aran Clary Deltac <bluefeet@gmail.com>
πΈ CONTRIBUTORS πΈ
- Xavier Guimard <x.guimard@free.fr>
- Mohammad S Anwar <mohammad.anwar@yahoo.com>
π LICENSE π
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. fr fr, go wild, just keep the same terms.
and that's the whole page bestie!! you ate that. πβ¨
this was MooseX...ngleArg(3pm) from perl v5.36.0, dated 2022-12-11 β mwah, go build something cute with it ππ¦π
π¦ where this page comes from
- debian bookworm: libmoosex-singlearg-perl 0.09-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-singlearg-perl 0.09-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-singlearg-perl 0.09-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-singlearg-perl 0.09-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-singlearg-perl 0.09-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms