Mo::default(3)
π Mo::default β the manual page, but make it cute π
okay bestie, so this is Mo::default(3) from the User Contributed Perl Documentation, and it's literally just here to add the default feature to Mo's has. that's it. that's the whole job. and she does it gorgeously. β¨
β¨ Name β¨
Mo::default β adds the default feature to Mo's has. she's a supporting character but she EATS every scene, no cap. π
πΈ Synopsis πΈ
use Mo qw'default';
has job => ( default => 'Killer' );
has name => ( default => sub { 'Joe' } );
has age => 42;
has colors => [ 'green', 'blue' ];
likeβ¦ look at that. every one of those has lines is getting a lil default value and they're all valid. iconic behavior honestly. π
π Description π
so what she does is add the default parameter to has, and that parameter expects a value. simple. elegant. main character energy.
and here's the vibe check on what that value can be:
- if the value is a code ref, it gets called to produce the default. so it does the work for you, like a good friend. π«Ά
- if the value is a hash ref or an array ref, then a shallow copy is made for the default. shallow. not deep. keep that in mind bestie, it's important. π
- if there's an odd number of arguments after the attribute name, then the first value is the default. sneaky but valid. π
and because she knows you're gonna ask β these 3 lines are literally the same thing:
has things => default => sub {+{}};
has things => default => {};
has things => {};
three different outfits, same slay. π
π¦ Laziness π¦
okay so this is the part where it gets interesting. default attribute values in Mo are lazy by default. meaning they chill until they're needed. very demure, very mindful. β¨
but you CAN change that! just explicitly set the lazy argument to false and it'll be initialized during instantiation instead. eager little thing.
use Mo qw'default';
has status => 'active'; # lazy
has location => sub { ... }, lazy => 1; # lazy
has target => sub { ... }, lazy => 0; # eager
see how lazy => 1 is lazy and lazy => 0 is eager? the numbers are telling on themselves. π
and if you wanna flip the whole default behavior β make attributes initialized eagerly by default β you just import nonlazy:
use Mo qw'default nonlazy';
has status => 'active'; # eager
has location => sub { ... }, lazy => 1; # lazy
has target => sub { ... }, lazy => 0; # eager
notice how now 'active' is eager by default, but lazy => 1 still overrides it back to lazy and lazy => 0 keeps it eager. the explicit flag always wins, periodt. π
ngl bestie,lazy => 1andlazy => 0are doing the heavy lifting here β if you want to override the default you HAVE to say so out loud. no guessing, no vibes-based inheritance. π
and that's the whole page!! no options list, no exit statuses, no environment variables, she's a short queen and she knows it. πΈ
this page comes from the <strong>perl v5.42.0</strong> distribution, dated <strong>2025-07-25</strong>, and lives at <strong>Mo::default(3)</strong> in the User Contributed Perl Documentation. bye bestie, go set some defaults, you're gonna do amazing π«Άππ§
π¦ where this page comes from
- fedora 43: perl-Mo 0.40-28.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms