On Fri, Jul 23, 2010 at 7:51 AM, Macdonald, John < john-421.macdon...@morganstanley.com> wrote:
> Two items: > > 1. I was just surprised to discover that Moose::Role does not allow > augmenting attributes. > > I was hoping to specify in the Role that an attribute was required, and > most of the characteristics of the attribute. In the class that uses the > role, it would augment the attribute to specify the actual value (with > default). However, I get a complaint about a required attribute needing a > default, handler, etc. when I do the "with Role", before I get a chance to > augment the attribute with its value. > > I wanted the required to be specified in the role to ensure that any class > that uses the role actually provided the default value (but didn't need to > do anything else to describe the attribute). > > Is there an alternate approach for this? > > > I think, you are looking for a builder - the more elegant approach to defaults In Role, specify the attribute and _require_ the builder method: requires 'default_foo'; has foo => (is => 'rw', ..., builder => 'default_foo' ); now in the class, implement default_foo, and you are in business. If you use the approach with 'requires' it'll even complain if the class didn't implement it. Alternatively, if there is a true default, omit requires and implement it in the Role to be overridden by classes. > 2. Is there a way of allowing a ref as a default value? I'm setting up a > class attribute - a 'ro' reference to a list of hashes, that the super-class > will access (using the subclasses list to drive the actions specific to the > subclass without requiring new code in each sub-class). I know I can use > default => sub { \$table } but I was hoping that there was some sort of flag > I could use to say that I really did want to have a common ref value shared > by all objects of this class. > > How about our $table = "Big Blob"; use constant table => \$table; in the attribute define 'table' as a builder You don't strictly speaking need use constant - a sub will do the same. I just think, constants are pretty if it's what you mean. - Kate