MooseX...:Sugar(3pm)

๐Ÿ“ฆ
version 1.000006 ยท 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’– MooseX::Has::Sugar ๐Ÿ’–

okay bestie, listen. this is a page from the User Contributed Perl Documentation (that's the manual family it lives in, MooseX...:Sugar(3pm) energy), and it's about making your Moose has declarations so much cuter and shorter. like, the way that => and quotes were living rent-free in your attribute blocks? we're evicting them. ๐Ÿ’…


โœจ NAME โœจ

MooseX::Has::Sugar โ€” Sugar Syntax for moose 'has' fields ๐Ÿ“


๐ŸŒธ VERSION ๐ŸŒธ

version 1.000006 โ€” yes queen, that's the one. ๐Ÿ’œ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

use Moose;
use MooseX::Types::Moose;
use MooseX::Has::Sugar;

has attrname      => ( isa => Str, ro, required   );
has otherattrname => ( isa => Str, rw, lazy_build );

like. look at that. no is => 'ro', no required => 1. it's giving minimalism, it's giving main character. ๐Ÿ‘‘


๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

MooseX::Has::Sugar and its related modules hand you simple, short-hand, bare-word functions that act as declarative macros for greatly compacting your Moose has declarations โ€” in a similar way to those provided by the declarative subroutines that MooseX::Types gives you. ๐Ÿซถ

so what does it actually provide? let me count the ways, bestie:

  • Less typing when defining has constraints ๐Ÿ’…
  • Faster, more skim-readable blocks of has constraints ๐Ÿ‘€
  • Perl Language Level syntax validation at compile time โญ

๐ŸŽ€ BENEFITS ๐ŸŽ€

๐Ÿ’– Reduced Typing in has declarations.

the constant need to type => and '' is fine for one-off cases, but the literal instant you have more than about 4 attributes it starts to get annoying. we know. we've been there. ๐Ÿ˜ญ

๐Ÿ’– More compact declarations.

this reduces a lot of the redundant typing in most cases, which makes your life easier, and makes it take up less visual space, which makes it faster to read. lowkey a glow-up for your source files. โœจ

๐Ÿ’– No String Worries

strings are often problematic, due to white-space etc. and like โ€” noted that if you do happen to mess them up, Moose should at least warn you that you've done something daft. but using this alleviates that worry entirely. no more playing string detective. ๐Ÿ•ต๏ธโ€โ™€๏ธ


๐Ÿ’€ COMPARISONS ๐Ÿ’€

๐Ÿ‘‘ Classical Moose

has foo => (
        isa => 'Str',
        is  => 'ro',
        required => 1,
);

has bar => (
        isa => 'Str',
        is => 'rw'
        lazy_build => 1,
);

๐Ÿ˜ญ Lazy Evil way to do it:

PLEASE DO NOT DO THIS

has qw( foo isa Str is ro required 1 );
has qw( bar isa Str is rw lazy_build 1 );
bestie I am BEGGING you. do not. this is not the vibe. this is the anti-vibe. ๐Ÿšซ

๐ŸŒธ With this module

( and with MooseX::Types )

use MooseX::Types::Moose qw( Str );
use MooseX::Has::Sugar;

has foo => (
        isa => Str,
        ro,
        required,
);
has bar => (
        isa => Str,
        rw,
        lazy_build,
);

Or even โœจ

use MooseX::Types::Moose qw( Str );
use MooseX::Has::Sugar;

has foo => ( isa => Str, ro,  required, );
has bar => ( isa => Str, rw,  lazy_build, );

okay but the second one is chef's kiss, one-liner-coded, ate and left no crumbs. ๐Ÿ“


๐Ÿงš ALTERNATIVE FORMS ๐Ÿงš

๐ŸŒŸ Basic is Expansion Only

( using ::Sugar::Minimal instead )

use MooseX::Types::Moose qw( Str );
use MooseX::Has::Sugar::Minimal;

has foo => (
        isa => Str,
        is  => ro,
        required => 1,
);
has bar => (
        isa => Str,
        is => rw,
        lazy_build => 1,
);

๐ŸŒŸ Attribute Expansions with Basic Expansions

( Combining parts of this and ::Sugar::Minimal )

use MooseX::Types::Moose qw( Str );
use MooseX::Has::Sugar::Minimal;
use MooseX::Has::Sugar qw( :attrs );

has foo => (
        isa => Str,
        is  => ro,
        required,
);
has bar => (
        isa => Str,
        is => rw,
        lazy_build,
);

mixing and matching? we love a flexible icon. ๐Ÿ’…


๐Ÿฆ„ EXPORT GROUPS ๐Ÿฆ„

๐Ÿ’œ :default

Since 0.0300, this exports all our syntax โ€” the same as :attrs :isattrs. primarily because I found you generally want all the sugar, not just part of it. this also gets rid of that nasty exclusion logic. periodt. โœจ

๐Ÿ’œ :isattrs

this exports ro, rw and bare as lists, so they behave as stand-alone attributes like lazy does. ๐ŸŽ€

has foo => (
        required,
        isa => 'Str',
        ro,
);
NOTE: This option is incompatible with ::Sugar::Minimal : CONFLICTS โš ๏ธ

๐Ÿ’œ :attrs

this exports lazy , lazy_build and required, coerce, weak_ref and auto_deref as subs that assume positive. ๐Ÿ‘‘

has foo => (
        required,
        isa => 'Str',
);
NOTE: This option is incompatible with MooseX::Types and Moose's Type Constraints Module : CONFLICTS โš ๏ธ

๐Ÿ’œ :is

DEPRECATED. See ::Sugar::Minimal for the same functionality. ๐Ÿ’€

๐Ÿ’œ :allattrs

DEPRECATED, just use :default or do:

use MooseX::Has::Sugar;

โญ EXPORTED FUNCTIONS โญ

  • bare โ€” returns ('is','bare') ๐Ÿฆ‹
  • ro โ€” returns ('is','ro') ๐Ÿ’–
  • rw โ€” returns ('is','rw') ๐ŸŒธ
  • required โ€” returns ('required',1) โœ…
  • lazy โ€” returns ('lazy',1) ๐Ÿ˜ด
  • lazy_build โ€” returns ('lazy_build',1) ๐Ÿ› ๏ธ
  • weak_ref โ€” returns ('weak_ref',1) ๐Ÿชถ
  • coerce โ€” returns ('coerce',1) ๐Ÿ”ฎ
WARNING: Conflict with MooseX::Types and Moose::Util::TypeConstraints, see CONFLICTS. ๐Ÿ˜ฌ
  • auto_deref โ€” returns ('auto_deref',1) โœจ

๐Ÿ’ฅ CONFLICTS ๐Ÿ’ฅ

๐Ÿ™ˆ MooseX::Has::Sugar::Minimal

๐Ÿ™ˆ MooseX::Has::Sugar::Saccharin

this module is not intended to be used in conjunction with ::Sugar::Minimal or ::Sugar::Saccharin. we export many of the same symbols and it's just not very sensible. like, why would you. ๐Ÿ˜ญ

๐Ÿ™ˆ MooseX::Types

๐Ÿ™ˆ Moose::Util::TypeConstraints

due to exporting the coerce symbol, using us in the same scope as a call to

use MooseX::Types ....

or

use Moose::Util::TypeConstraints

will result in a symbol collision. ๐Ÿ’€

we recommend using and creating proper type libraries instead, ( which will absolve you entirely of the need to use MooseX::Types and MooseX::Has::Sugar(::*)? in the same scope ) ๐Ÿซถ


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Kent Fredric <kentnl@cpan.org> ๐Ÿ’œ


This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>. ๐ŸŒธ

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. โœจ


and that's the whole page bestie, no cap. this one came from perl v5.34.0, dated 2022-06-15 โ€” so if you ever need to cite your sources in the group chat, that's the receipt. ๐Ÿงพ๐Ÿ’… ily, go write some cute has blocks. ๐Ÿซถ๐Ÿ’–

๐Ÿ“ฆ where this page comes from