Adding default build through trait

[email protected] ("Ian Sillitoe")
Newsgroups perl.moose
Message-ID <[email protected]>
Apologies in advance if this is a dumb question but I haven't been able to
figure it out from the examples and my head is now fairly fried from all the
meta poking. It seems a fairly simple thing - so I guess I'm missing
something obvious (I'll happily write the answer up if it isn't already in
the docs somewhere).

I'm trying to create a trait that will provide a default build method if one
hasn't already been set (well it will do more than that, but this is the bit
I'm trying to figure out).

So:

has 'result' => (
  traits => [ 'MyTrait' ],
  is => 'rw',
  isa => 'Str',
);

Would effectively be changed to:

has 'result' => (
  traits => [ 'MyTrait' ],
  is => 'rw',
  isa => 'Str',
  default => sub { "built from default subroutine provide by MyTrait" }
);

But the following would be left alone:

has 'result' => (
  traits => [ 'MyTrait' ],
  is => 'rw',
  isa => 'Str',
  default => sub { "built from custom method" }
);

I can get this to work by getting the trait to provide a custom attribute
(e.g. 'mytrait_build') that effectively does the same job as
build/default/initialize (and just getting the user to specifically override
that), but it seemed neater to try and reuse the existing attributes if
possible.

Many thanks




-- 
Ian Sillitoe
CATH Team -- http://cathdb.info
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.