Hi,

I'm new to Moose.  I've been tinkering with a toy example to learn Moose.  My 
goal is
to make my Moose extension as syntactically elegant as possible.

The basic idea is to have a trait called "Cached" which has a timestamp, which 
would
be checked and then cause invalidation.  The Cached Trait would add a Timer 
attribute
to the attribute, as well as an around method to the attribute reader, and a 
manual
method (called "invalidate_my_cached_attribute").

I found using the MOP pretty self explanatory, but I did run into one problem.  
I couldn't
find a way to hook everything in simply using a trait.  I had to separate the 
Trait defining
stuff (Cached Trait, timer) from the Exporter stuff (around method, add 
invalidate method).

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).

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.

I'm sure there's a way to do this, I'm just missing a technique or two here.

Thanks,

Roger

Reply via email to