MooseX...:Sugar(3pm)
๐ 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
hasconstraints ๐ - Faster, more skim-readable blocks of
hasconstraints ๐ - 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 withMooseX::Typesand 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 withMooseX::TypesandMoose::Util::TypeConstraints, seeCONFLICTS. ๐ฌ
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> ๐
๐ COPYRIGHT AND LICENSE ๐
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
- debian bookworm: libmoosex-has-sugar-perl 1.000006-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-has-sugar-perl 1.000006-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-has-sugar-perl 1.000006-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-has-sugar-perl 1.000006-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-has-sugar-perl 1.000006-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms