Mo::default(3)

πŸ“¦
version 0.40 Β· 2 versions
, in fedora 43

πŸ’– 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 => 1 and lazy => 0 are 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