The following doesn't work.  The idea is to add a 'Cached' trait by calling
the default 'clearer' routine on an attribute.  The reason it doesn't work
is that when it is invoked, there is no clearer routine.  The code dies
with the stack trace saying it doesn't exist on the object (the attribute).
However the _name_ of the clearer routine is known at this point.

use Moose::Role;

around accessor_metaclass => sub
{
    my $orig = shift;
    my $self = shift;

    my $timer = $self->expire_timer;
    if ($timer->reset_if_expired)
    {
        my $clearer = $self->clearer;
        $self->$clearer ();
    }

    return $self->$orig (@_);
};


I'm guessing that the object is in a partially constructed state at this point,
and this is invoked prior to the addition of the clearer routine.

Note that I can take the same code and change it a little bit, and make
it part of a class (i.e. use Moose).  I'm required to first find the attribute
and then invoke this same code (i.e. $self = $instance->meta->get_attribute 
('my-attribute')).

Is there a way to hook the gettor from within a Role?  This seems more natural 
since
the code applies solely to the attribute (as opposed to putting it on the Class 
and passing
in the attribute name).

Thanks,

Roger

Reply via email to