Looking over that bit of code did the trick. A simple role did the trick. role { around accessor_metaclass => sub { ... } };
Thanks, Roger ----- Original Message ---- From: Jesse Luehrs <d...@tozt.net> To: moose@perl.org Sent: Sat, November 7, 2009 11:53:20 AM Subject: Re: Combining Trait Definition and Exporter In Same Module File On Sat, Nov 07, 2009 at 08:03:14AM -0800, roger bush wrote: > Here's how you currently need to use my trait: > > > use MyCachedAttributeUtil; > > has 'my_cached_attribute' => ( > isa => 'ArrayRef[MyClass]', > traits => ['Cached'], > is => 'ro', > lazy_build => 1, > init_arg => undef, > expire_seconds => 1, > ); > > cached 'my_cached_attribute'; > > > It seems there should be a way to eliminate the first line and last line, and > have the trait > (Cached) do everything. But I couldn't get this to work. > > There seem to be two issues: > > 1. The Cached Traits implementation is passed an anonymous class object, > which does not > give me access to the reader routine (in this case my_cached_attribute is > the default > reader). While I can get the name of the method, I wasn't actually able > to modify the > method by applying the "around" modifier. I solved this by putting > the around modifier > in the separate util module "MyCachedAttributeUtil", and adding it to the > class in the > call "cached 'my_cached_attribute'. > > I think the problem was I needed to "go up one level", because the symbol > I needed > to modify ('my_cached_attribute'), would have not been at the attribute > level (the > implementation would have been, but the name of the method is possibly > one level up). What you probably need to do here is modify how the accessor is generated. MooseX::AttributeTree might be a good example of how to get started. > 2. When I attempt to put both the Moose::Export stuff (from > MyCachedAttributeUtil) in > the same module as the Moose::Role stuff (Cached Trait), things are > unhappy. > The exact error is that the new keyword "cached" which I export, doesn't > seem to > be recognized. Yeah, probably don't do this. Keeping stuff in separate files makes things cleaner all around. -doy