develooper Front page | perl.moose | Postings from March 2016

Re: custom accessor_metaclass override of _inline_store (Moose 1 to2)

Thread Previous | Thread Next
From:
Toby Blake
Date:
March 22, 2016 12:42
Subject:
Re: custom accessor_metaclass override of _inline_store (Moose 1 to2)
Message ID:
3335CCB4-B95A-45F1-8CB0-2E6EC493DA9C@inf.ed.ac.uk
> On 15 Mar 2016, at 16:13, Dave Rolsky <autarch@urth.org> wrote:
> 
> On Tue, 15 Mar 2016, Toby Blake wrote:

[...]

>> override _inline_store => sub {
>> my ($self, $instance, $value) = @_;
>> my $attr = $self->associated_attribute;
>> if ($attr->has_logger) {
>>    return sprintf('$attr->logger->($attr, %s, %s);', $instance, $value) . super;
>> }
>> return super;
>> };
>> 
>> So, essentially, it puts a call to the attribute trait's $attr->logger
>> subroutine in the chain when setting a value (this is needed because we
>> have code here which needs to know when an object's attribute values have
>> changed - this is the function of the "logger").  And of course this code
>> doesn't work with Moose 2, as there is no longer an _inline_store method in
>> Moose::Meta::Method::Accessor.

[...]

> I think you can override Moose::Meta::Attribute->_inline_instance_set. This will work for any attribute that doesn't have an initializer (but no one uses that Moose feature).

I wasn't able to get this working using
Moose::Meta::Attribute->_inline_instance_set, and the more time I spent
grubbing about in Moose and Class::MOP's internals the more I became convinced
that it was the wrong way to do it.

I _think_ I can achieve what I want by using trigger methods (it seems that
when the project was originally written, triggers didn't have access to the
old value, which I'm presuming is why the original author went with the
approach he did).  However, this leads to the following question...

All these attributes to which I want to apply this trigger share the same
trait, so there's a logical association between the two.  Is there a way I
can utilise this, so I don't have to define 'trigger => ....' for every
attribute which uses this trait, i.e.  have a trigger built into an
attribute trait, if that makes sense?

Cheers
Toby


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About