| Moose-Micro documentation | Contained in the Moose-Micro distribution. |
Moose::Micro - succinctly specify Moose attributes
version 0.002
package MyClass; use Moose::Micro 'foo $bar @baz; %!quux';
Moose::Micro makes it easy to declare Moose attributes without a lot of typing.
The argument to use Moose::Micro is a list of attribute names, which is
split on whitespace. Any attributes named before the (optional) semicolon are
required; any after it are not.
Sigils are optional, and impose the following type constraints:
@: ArrayRef%: HashRef$: anything under Defined that isn't one of the aboveNo sigil means no type constraint.
Following the sigil or prefixing the attribute name with ! makes the
attribute 'private'; that is, the generated accessor will start with _,
e.g.:
!foo $!bar
If your class has a method named _build_$attribute, lazy_build => 1
is added to the attribute definition.
All attributes are declared is => 'rw'.
There is no way to specify many options, like default, builder, handles, etc.
These are all internals that you probably don't care about. They'll be documented when they're stable.
Hans Dieter Pearcey <hdp@cpan.org>
This software is copyright (c) 2009 by Hans Dieter Pearcey. This is free software; you can redistribute it and/or modify it under the same terms as perl itself.
| Moose-Micro documentation | Contained in the Moose-Micro distribution. |
use strict; use warnings; package Moose::Micro; our $VERSION = '0.002'; use Moose (); use Moose::Exporter; use B::Hooks::EndOfScope; my ($import, $unimport); BEGIN { ($import, $unimport) = Moose::Exporter->build_import_methods( also => 'Moose', ); } sub import { my $class = shift; my $attributes = shift; my $caller = caller; on_scope_end { my $meta = Moose::Meta::Class->initialize($caller); $meta->add_attribute(@$_) for $class->attribute_list($caller, $attributes); }; unshift @_, $class; goto &$import; } sub unimport { goto &$unimport } sub attribute_list { my ($self, $pkg, $attributes) = @_; my @attributes; my ($required, $optional) = split /\s*;\s*/, $attributes; for my $attr (grep { length } split /\s+/, $required) { my ($name, %args) = $self->attribute_args($pkg, $attr); $args{required} = 1; push @attributes, [ $name, %args ]; } for my $attr (grep { length } split /\s+/, $optional) { my ($name, %args) = $self->attribute_args($pkg, $attr); push @attributes, [ $name, %args ]; } return @attributes; } sub attribute_args { my ($self, $pkg, $attribute) = @_; my %args = ( is => 'rw', ); if ($attribute =~ s/^([\$\@\%])//) { my $type = $1; %args = (%args, $self->type_constraint_for($type)); } if ($attribute =~ s/^\!//) { %args = (%args, accessor => "_$attribute"); } if ($pkg->can("_build_$attribute")) { $args{lazy_build} = 1; } return ($attribute => %args); } my %TC = ( '$' => 'Value|ScalarRef|CodeRef|RegexpRef|GlobRef|Object', '@' => 'ArrayRef', '%' => 'HashRef', ); sub type_constraint_for { my ($self, $sigil) = @_; return (isa => $TC{$sigil}); } 1; __END__