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

Reply via email to