simultaneous defined attributes and triggers

[email protected] (Christopher Brown)
Newsgroups perl.moose
Message-ID <[email protected]>
Hi Moose Folks,

Perhaps there is a quick answer here, but I don't see one.  Suppose I want
to create an attribute that has a trigger that sets the value of the
attribute 2higher than the input.  It is easily done.

has 'foo' => (
        is => 'rw' ,
        isa => 'Int' ,
        trigger => sub {
            $_[0]->{foo} = $_[1]+2 ;
        } ,
    );

...
$object->foo(2);
print $foo;   # 4

Now, suppose that I use the syntax for defining attributes simultaneously,

    has [ qw(foo bar ) ] => (
        is => 'rw' ,
        isa => 'Int' ,
        trigger => sub {
            $_[0]->{foo} = $_[1]+2 ;
                                           # ^^^^
        } ,
    );


      $object->bar(2);
      print $object->bar;  # 2  wrong
      print $object->foo;  # 4  wrong

Is there a simple way around this, i.e. to use simultaneous defined
attributes with triggers?  I suppose that I can use the caller function to
get the name of the method, but I suspect that someone has found a better
solution.

If there is, I would love to hear it.

Chris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.