| MooseX-Has-Sugar documentation | Contained in the MooseX-Has-Sugar distribution. |
MooseX::Has::Sugar - Sugar Syntax for moose 'has' fields
version 0.05070419
Moose has syntax is generally fine, but sometimes one gets bothered with
the constant typing of string quotes for things. The MooseX::Types module exists and in
many ways reduces the need for constant string creation.
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.
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.
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.
has foo => (
isa => 'Str',
is => 'ro',
required => 1,
);
has bar => (
isa => 'Str',
is => 'rw'
lazy_build => 1,
);
PLEASE DO NOT DO THIS
has qw( foo isa Str is ro required 1 );
has qw( bar isa Str is rw lazy_build 1 );
( 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, );
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,
);
( 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,
);
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.
This exports ro, rw and bare as lists, so they behave as stand-alone attrs like
lazy does.
has foo => (
required,
isa => 'Str',
ro,
);
NOTE: This option is incompatible with ::Sugar::Minimal : CONFLICTS
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
DEPRECATED. See ::Sugar::Minimal for the same functionality
DEPRECATED, just use :default or do
use MooseX::Has::Sugar;
returns ('is','bare')
returns ('is','ro')
returns ('is','rw')
returns ('required',1)
returns ('lazy',1)
returns ('lazy_build',1)
returns ('weak_ref',1)
returns ('coerce',1)
WARNING: Conflict with MooseX::Types and Moose::Util::TypeConstraints, see CONFLICTS.
returns ('auto_deref',1)
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.
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 )
Kent Fredric <kentnl at cpan.org>
This software is copyright (c) 2011 by Kent Fredric.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
| MooseX-Has-Sugar documentation | Contained in the MooseX-Has-Sugar distribution. |
use warnings; use strict; package MooseX::Has::Sugar; BEGIN { $MooseX::Has::Sugar::VERSION = '0.05070419'; } # ABSTRACT: Sugar Syntax for moose 'has' fields use Carp (); use Sub::Exporter (); Sub::Exporter::setup_exporter( { as => 'do_import', exports => [ 'ro', 'rw', 'required', 'lazy', 'lazy_build', 'coerce', 'weak_ref', 'auto_deref', 'bare', ], groups => { isattrs => [ 'ro', 'rw', 'bare', ], attrs => [ 'required', 'lazy', 'lazy_build', 'coerce', 'weak_ref', 'auto_deref', ], default => [ '-attrs', '-isattrs' ], } } ); sub import { for (@_) { if ( $_ =~ qr/^[:-]is\$/ ) { Carp::croak( qq{Trivial ro/rw with :is dropped as of 0.0300.\n} . q{ See MooseX::Has::Sugar::Minimal for those. } ); } if ( $_ =~ qr/^[:-]allattrs\$/ ) { Carp::carp(q{MooseX::Has::Sugar->import(:allattrs) is deprecated. just do 'use MooseX::Has::Sugar;' instead.}); $_ =~ s/^[:-]allattrs\$/:default/; } } goto &MooseX::Has::Sugar::do_import; } sub bare() { return ( 'is', 'bare' ); } sub ro() { return ( 'is', 'ro' ); } sub rw() { return ( 'is', 'rw' ); } sub required() { return ( 'required', 1 ); } sub lazy() { return ( 'lazy', 1 ); } sub lazy_build() { return ( 'lazy_build', 1 ); } sub weak_ref() { return ( 'weak_ref', 1 ); } sub coerce() { return ( 'coerce', 1 ); } sub auto_deref() { return ( 'auto_deref', 1 ); } 1; __END__