logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

MooseX::Has::Sugar - Sugar Syntax for moose 'has' fields

Alternative Forms

Basic"is"ExpansionOnly
       ( 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,
           );

   AttributeExpansionswithBasicExpansions
       ( 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,
           );

Author

       Kent Fredric <kentnl@cpan.org>

Benefits

ReducedTypingin"has"declarations.
       The constant need to type "=>" and '' is fine for one-off cases, but the instant you have more than about
       4 attributes it starts to get annoying.

   Morecompactdeclarations.
       Reduces  much  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.

   NoStringWorries
       Strings are often problematic, due to white-space etc. Noted that if you do happen to mess them up, Moose
       should at least warn you that you've done something daft. Using this alleviates that worry.

Comparisons

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

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

   LazyEvilwaytodoit:PLEASEDONOTDOTHIS

           has qw( foo isa Str is ro required 1 );
           has qw( bar isa Str is rw lazy_build 1 );

   Withthismodule
       ( 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, );

Conflicts

MooseX::Has::Sugar::MinimalMooseX::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 its just not very sensible.

   MooseX::TypesMoose::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 )

Description

       "MooseX::Has::Sugar" and its related modules provide simple, short-hand, bare-word functions that act as
       declarative macros for greatly compacting "Moose" "has" declarations, in a similar way to those provided
       by the declarative subroutines provided by "MooseX::Types"

       This provides:

       •   Less typing when defining "has" constraints

       •   Faster, more skim-readable blocks of "has" constraints

       •   Perl Language Level syntax validation at compile time

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.

   ":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:Thisoptionisincompatiblewith::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:ThisoptionisincompatiblewithMooseX::TypesandMoose'sTypeConstraintsModule : "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)"

Name

       MooseX::Has::Sugar - Sugar Syntax for moose 'has' fields

Synopsis

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

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

Version

       version 1.000006

See Also