From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

MooseX::AttributeDefaults - Role to provide default option for your attribute metaclasses

VERSION

Version 0.02

SYNOPSIS

Although you can do similar things by overriding attributes in subclasses of Moose::Meta::Attribute, there are a couple of gotchas (as of this writing, for instance, overriding 'is' does nothing at all). This role abstracts the implementation details of the available workarounds.

use Moose;
sub default_options {
my ($class, $name) = @_;
return (
is => 'ro',
isa => 'Str',
default => "default value for $name";
);
}
package Some::Class;
use Moose;
has 'attr' => (
metaclass => 'My::Custom::Metaclass',
predicate => 'has_attr',
);
# 'attr' is a ro string with "default value for attr" as its
# default and a 'has_attr' predicate
### Or as a trait instead of a metaclass
sub default_options {
is => 'ro',
isa => 'ArrayRef',
default => sub { [] },
}
package Some::Class;
use Moose;
has attr => (
metaclass => 'Collection::Array',
traits => [qw(Acme::Common::Array)],
provides => {
'push' => 'add_attr',
},
);

REQUIRED METHODS

default_options

Return a list of options to default to. This is called as a class method with the attribute name as its only argument.

AUTHOR

Paul Driver, <frodwith at cpan.org>

COPYRIGHT & LICENSE

Copyright (C) 2008 Paul Driver.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.