MooseX:...hortcuts(3)

๐Ÿ“ฆ
version 0.35 ยท 2 versions
, in fedora 43

โœจ NAME โœจ

MooseX::Extended::Manual::Shortcuts โ€” aka the manual page for MooseX::Extended that hands you a whole bag of shortcuts so writing your Moose code is lowkey effortless ๐Ÿ’…

It's MooseX:...hortcuts(3), from the User Contributed Perl Documentation, and bestie, it's giving "quality of life update" fr fr.


๐ŸŒธ VERSION ๐ŸŒธ

version 0.35


๐ŸŽ€ ATTRIBUTE SHORTCUTS ๐ŸŽ€

Okay so when you're using field or param, the module is like "here, let me carry that for you" and gives you a bunch of attribute shortcuts:

param name => (
    isa       => NonEmptyStr,
    writer    => 1,   # set_name
    reader    => 1,   # get_name
    predicate => 1,   # has_name
    clearer   => 1,   # clear_name
    builder   => 1,   # _build_name
);

sub _build_name ($self) {
    ...
}

And these hit the same when you pass an array reference to the function, no cap:

package Point {
    use MooseX::Extended types => ['Int'];

    param [ 'x', 'y' ] => (
        isa     => Int,
        clearer => 1,     # clear_x and clear_y available
        default => 0,
    );
}

So like โ€” these are shortcuts, and they exist to make attributes easier to write and way more consistent. That's the vibe. But you can absolutely still use the full names if you want to be extra:

field authz_delegate => (
    builder => '_build_my_darned_authz_delegate',
);

These are very similar to [MooseX::AttributeShortcuts](https://metacpan.org/pod/MooseX::AttributeShortcuts), but heads up, the naming is a little different. Example time: clearer => 1 for a _name attribute creates a clear__name method. But for MooseX::AttributeShortcuts, it would have been named _clear_name. Different name, same energy, don't get it twisted ๐Ÿซถ

You still have the has function available for defining attributes, but it is unchanged. These shortcuts will not work with it. It's just not that girl.

โœ๏ธ writer โœ๏ธ

If an attribute has writer set to 1 (the number one, not a truthy-ish whatever), a method named set_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    writer => 1,
);

Is the same as this:

has title => (
    is      => 'rw',                  # we change this from 'ro'
    isa     => Undef | NonEmptyStr,
    default => undef,
    writer  => 'set_title',
);

๐Ÿ‘€ reader ๐Ÿ‘€

By default, the reader (accessor) for the attribute is the same as the name. You can always change this:

has payload => ( is => 'ro', reader => 'the_payload' );

However, if you want to change the reader nameโ€ฆ

If an attribute has reader set to 1 (the number one), a method named get_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    reader => 1,
);

Is the same as this:

has title => (
    is      => 'rw',                  # we change this from 'ro'
    isa     => Undef | NonEmptyStr,
    default => undef,
    reader  => 'get_title',
);

๐Ÿ” predicate ๐Ÿ”

If an attribute has predicate set to 1 (the number one), a method named has_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    predicate => 1,
);

Is the same as this:

has title => (
    is        => 'ro',
    isa       => Undef | NonEmptyStr,
    default   => undef,
    predicate => 'has_title',
);

๐Ÿงผ clearer ๐Ÿงผ

If an attribute has clearer set to 1 (the number one), a method named clear_$attribute_name is created.

This:

param title => (
    isa     => Undef | NonEmptyStr,
    default => undef,
    clearer => 1,
);

Is the same as this:

has title => (
    is      => 'ro',
    isa     => Undef | NonEmptyStr,
    default => undef,
    clearer => 'clear_title',
);

๐Ÿ—๏ธ builder ๐Ÿ—๏ธ

If an attribute has builder set to 1 (the number one), a method named _build_$attribute_name.

This:

param title => (
    isa     =>  NonEmptyStr,
    builder => 1,
);

Is the same as this:

has title => (
    is      => 'ro',
    isa     => NonEmptyStr,
    builder => '_build_title',
);

Obviously, a "private" attribute, such as _auth_token would get a build named _build__auth_token (note the two underscores between build and auth_token). Yeah. Two of them. That's just how it do ๐Ÿ’€


๐Ÿฎ STEALING FROM MOO ๐Ÿฎ

๐Ÿ” is => 'rwp' ๐Ÿ”

param name => ( is => 'rwp' );

The above is equivalent to:

has name => ( is => 'ro', writer => '_set_name' );

Of course, it works for field, too. Equal opportunity and all that โœจ

๐Ÿงฌ builder Code References ๐Ÿงฌ

You may also pass a coderef to builder:

field created => (
    isa     => PositiveInt,
    lazy    => 0,
    builder => sub {time},
);

This is different from default => sub {...} because it will install a _build_created method for you. This is useful if you with to allow a subclass to override this method. Slay, honestly. Inheritance queen behavior ๐Ÿ‘‘


๐Ÿฉบ DIAGNOSTICS ๐Ÿฉบ

Attributes defined using param or field which are read-only with no init_arg and no default or builder, will result in a warning. If you wish to disable this warning you can.

no warnings 'MooseX::Extended::naked_fields';
Heads up bestie: this warning is only available on Perl >= 5.028. Older Perls simply won't be participating, sorry ๐Ÿ˜ญ

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Curtis "Ovid" Poe <curtis.poe@gmail.com>


This software is Copyright (c) 2022 by Curtis "Ovid" Poe.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

That's the whole page, bestie, you ate it up ๐Ÿ’– This was MooseX:...hortcuts(3) from the User Contributed Perl Documentation, perl v5.42.0, dated 2025-07-25 โ€” go forth and write your attributes the easy way, periodt ๐Ÿฆ‹๐ŸŒธ๐ŸŽ€

๐Ÿ“ฆ where this page comes from