Emulate class::accessor::fast behavior using moose attributes
package MyClass; use Moose; with 'MooseX::Emulate::Class::Accessor::Fast'; #fields with readers and writers _\|_PACKAGE_\|_->mk_accessors(qw/field1 field2/); #fields with readers only _\|_PACKAGE_\|_->mk_ro_accessors(qw/field3 field4/); #fields with writers only _\|_PACKAGE_\|_->mk_wo_accessors(qw/field5 field6/);
This module attempts to emulate the behavior of Class::Accessor::Fast as accurately as possible using the Moose attribute system. The public \s-1API\s0 of \*(C`Class::Accessor::Fast\*(C' is wholly supported, but the private methods are not. If you are only using the public methods (as you should) migration should be a matter of switching your \*(C`use base\*(C' line to a \*(C`with\*(C' line.
While I have attempted to emulate the behavior of Class::Accessor::Fast as closely as possible bugs may still be lurking in edge-cases.
Simple documentation is provided here for your convenience, but for more thorough documentation please see Class::Accessor::Fast and Class::Accessor.
Please note that, at this time, the \*(C`is\*(C' flag attribute is not being set. To determine the \*(C`reader\*(C' and \*(C`writer\*(C' methods using introspection in later versions of Class::MOP ( > 0.38) please use the \*(C`get_read_method\*(C' and \*(C`get_write_method\*(C' methods in Class::MOP::Attribute. Example
# with Class::MOP <= 0.38 my $attr = $self->meta->find_attribute_by_name($field_name); my $reader_method = $attr->reader || $attr->accessor; my $writer_method = $attr->writer || $attr->accessor;
# with Class::MOP > 0.38 my $attr = $self->meta->find_attribute_by_name($field_name); my $reader_method = $attr->get_read_method; my $writer_method = $attr->get_write_method;
Change the default Moose class building to emulate the behavior of C::A::F and store arguments in the instance hashref. Create read-write accessors. An attribute named $field_name will be created. The name of the c<reader> and \*(C`writer\*(C' methods will be determined by the return value of \*(C`accessor_name_for\*(C' and \*(C`mutator_name_for\*(C', which by default return the name passed unchanged. If the accessor and mutator names are equal the \*(C`accessor\*(C' attribute will be passes to Moose, otherwise the \*(C`reader\*(C' and \*(C`writer\*(C' attributes will be passed. Please see Class::MOP::Attribute for more information. Create read-only accessors. Create write-only accessors.
Preface readers with 'get_' and writers with 'set_'. See original Class::Accessor documentation for more information.
See original Class::Accessor documentation for more information.
See original Class::Accessor documentation for more information.
See original Class::Accessor documentation for more information.
See Moose::Meta::Class.
Moose, Moose::Meta::Attribute, Class::Accessor, Class::Accessor::Fast, Class::MOP::Attribute, MooseX::Adopt::Class::Accessor::Fast
Guillermo Roditi (groditi) <[email protected]>
With contributions from:
You may distribute this code under the same terms as Perl itself.