Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I've been learning Moose and I came across unexpected behavior: method modifiers (such as 'after') do not seem to work with attribute triggers. This is an issue because I want a subclass to extend a method that is defined as a trigger in the parent. The issue and one workaround is shown below.

use strict; use warnings; package Foo; use Moose; has 'attrib' => ( is => 'rw', trigger => \&attrib_changed ); has 'workaround' => ( is => 'rw', trigger => \&workaround_changed ); sub attrib_changed { print " in attrib_changed\n"; # called } after 'attrib_changed' => sub { print " in 'after' attrib_changed\n"; # not called }; sub workaround_changed { my ( $self ) = @_; print " in workaround_changed\n"; # called $self->not_a_trigger; } after 'workaround_changed' => sub { print " in 'after' workaround_changed\n"; # not called }; sub not_a_trigger { print " in not_a_trigger\n"; # called } after 'not_a_trigger' => sub { print " in 'after' not_a_trigger\n"; # called }; my $foo = Foo->new; print "Calling attrib( 1 ):\n"; $foo->attrib( 1 ); print "Calling workaround( 1 ):\n"; $foo->workaround( 1 );
Output: Calling attrib( 1 ): in attrib_changed Calling workaround( 1 ): in workaround_changed in not_a_trigger in 'after' not_a_trigger

I could not find anything in the Moose docs or online that documents this behavior as a known limitation. I'd be surprised if I were the first one to notice it, so it makes me wonder whether I am trying to do something that is best done some other way. Thoughts?


Update: Cross-posted to

In reply to Moose triggers & method modifiers by bobf

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2023-03-23 11:34 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.