Mo::build(3)

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

πŸ’– Mo::build πŸ’–

okay bestie, so this is Mo::build from the User Contributed Perl Documentation section of the manual (manual page Mo::build(3), iykyk). It's the lil add-on that gives Mo its ✨build✨ era.


🌸 NAME 🌸

Mo::build β€” adds the build feature to Mo πŸŽ€


✨ SYNOPSIS ✨

use Mo qw'build';
has name => ( is => 'rw' );

sub BUILD {
    my $self = shift;
    ...
}

πŸ¦‹ DESCRIPTION πŸ¦‹

So the way this works is: the moment you import it, it adds the BUILD feature to Mo. That's the whole vibe. πŸ’…

If there's a sub called BUILD hangin' out on the package, it gets run on $self during instantiation. Like, right in the middle of the whole "new object just dropped" moment. πŸ‘‘

And here's the tea: any non-lazy default and builder attributes, plus anything you passed into new, are already set by the time BUILD gets called. So you can just reach in and read them, no stress. 🫢

package ABCD;
use Mo qw'build builder default';
use feature 'say';

has a => (default => 1234, lazy => 0);
has b => (builder => '_b', lazy => 0);
has c => (is => 'rw');
has d => (is => 'rw');

sub _b { 'blue' }

sub BUILD {
    my ($self) = @_;
    say $self->{a};
    say $self->{b};
    say $self->{c};
    say 'undef' unless defined $self->{d};
}

ABCD->new(c => 'days') # => 1234
                       #    blue
                       #    days
                       #    undef

like... a shows up as 1234 because of the default, b is blue from the builder, c is days 'cause we passed it to new, and d is just out here being undef 😭 not me crying over an undefined attribute. The lazy => 0 on a and b is literally why they're ready in time β€” non-lazy stays on business. ⭐


πŸ’€ reminder bestie: only non-lazy default/builder stuff is guaranteed to be set when BUILD fires. Don't come for me if your lazy attributes are still napping. πŸ™ˆ

perl v5.42.0 Β· 2025-07-25 β€” mwah, that's the version and the date this lil page was born, shoutout to the perl folks, and slay, you made it to the end, go build something iconic πŸ§šπŸ’œ

πŸ“¦ where this page comes from