NAME
MooseX::FunkyAttributes::Role::Attribute - custom get/set/clear/has coderefs
SYNOPSIS
package Circle;
use Moose;
use MooseX::FunkyAttributes;
has radius => (
is => 'rw',
isa => 'Num',
predicate => 'has_radius',
);
has diameter => (
traits => [ FunkyAttribute ],
is => 'rw',
isa => 'Num',
custom_get => sub { 2 * $_->radius },
custom_set => sub { $_->radius( $_[-1] / 2 ) },
custom_has => sub { $_->has_radius },
);
DESCRIPTION
This is the base trait which the other MooseX::FunkyAttribute traits inherit from. It allows you to provide coderefs to handle the business of storing and retrieving attribute values.
So instead of storing your attribute values in the object's blessed hashref, you could calculate them on the fly, or store them in a file or database, or an external hashref, or whatever.
Options
If your attribute uses this trait, then you must provide at least the following three coderefs:
custom_set => CODE ($meta, $instance, $value)
-
The code which implements setting an attribute value. Note that this code does not need to implement type constraint checks, etc.
$meta
is a Moose::Meta::Attribute object describing the attribute;$instance
is the object itself.$_
is available as an alias for the instance. custom_get => CODE ($meta, $instance)
-
The code which implements getting an attribute value.
It should return the value.
custom_has => CODE ($meta, $instance)
-
The code which implements the predicate functionality for an attribute. That is, it should return true if the attribute has been set, and false if the attribute is unset. (Note that Moose does allow attribute values to be set to undefined, so settedness is not the same as definedness.)
The following three additional coderefs are optional:
custom_clear => CODE ($meta, $instance)
-
The code which clears an attribute value, making it unset.
If you do not provide this, then your attribute cannot be cleared once set.
custom_init => CODE ($meta, $instance, $value)
-
Like
custom_set
but used during object construction.If you do not provide this, then the
custom_set
coderef will be used in its place. custom_weaken => CODE ($meta, $instance)
-
The code which weakens an attribute value that is a reference.
If you do not provide this, then your attribute cannot be a weak ref.
Moose attempts to create inlined attribute accessors whenever possible. The following coderefs can be defined which must return strings of Perl code suitable for inlining the accessors. They are each optional, but unless all of them are defined, your attribute will not be inlined.
custom_inline_set => CODE ($meta, $instance_string, $value_string)
-
$instance_string
is a string representing the name of the instance variable, such as"\$self"
.$value_string
is a string which evaluates to the value.An example for the
diameter
example in the SYNOPSIScustom_inline_set => sub { my ($meta, $i, $v) = @_; return sprintf('%s->{radius} = (%s)/2', $i, $v); },
custom_inline_get => CODE ($meta, $instance_string)
-
An example for the
diameter
example in the SYNOPSIScustom_inline_get => sub { my ($meta, $i) = @_; return sprintf('%s->{radius} * 2', $i); },
custom_inline_has => CODE ($meta, $instance_string)
-
An example for the
diameter
example in the SYNOPSIScustom_inline_has => sub { my ($meta, $i) = @_; return sprintf('exists %s->{radius}', $i); },
custom_inline_clear => CODE ($meta, $instance_string)
-
An example for the
diameter
example in the SYNOPSIScustom_inline_has => sub { my ($meta, $i) = @_; return sprintf('delete %s->{radius}', $i); },
custom_inline_weaken => CODE ($meta, $instance_string)
-
An example for the
diameter
example in the SYNOPSIScustom_inline_has => sub { my ($meta, $i) = @_; return sprintf('Scalar::Util::weaken(%s->{radius})', $i); },
(Not that weakening a Num makes any sense...)
Your attribute metaobject has the following methods (in addition to the standard Moose::Meta::Attribute stuff):
custom_get
custom_set
custom_has
custom_clear
,has_custom_clear
custom_weaken
,has_custom_weaken
custom_init
,has_custom_init
custom_inline_get
,has_custom_inline_get
custom_inline_set
,has_custom_inline_set
custom_inline_has
,has_custom_inline_has
custom_inline_clear
,has_custom_inline_clear
custom_inline_weaken
,has_custom_inline_weaken
accessor_should_be_inlined
has_all_inliners
BUGS
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooseX-FunkyAttributes.
SEE ALSO
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCE
This software is copyright (c) 2012-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.