I have a Moose class that
has 'unique_ints' => (
is => 'rw',
isa => 'ArrayRef[Int]',
default => sub { [] },
);
Which is the best way to guarantee unique_int's uniqueness?
I have a Moose class that
has 'unique_ints' => (
is => 'rw',
isa => 'ArrayRef[Int]',
default => sub { [] },
);
Which is the best way to guarantee unique_int's uniqueness?
Copyright © 2021 Jogjafile Inc.
Moose attributes can have a
triggerproperty which is called whenever that attribute is changed. The property is a reference to a method that is passed the object and the new and old versions of the attribute. So we can write something like this:Note that in the trigger method, we're using direct hash access to the attribute's value, as calling the mutator again would call the trigger method again and get us into an infinite pit of recursion.
We can test the method like this:
Which, as expected, produces this output:
Update: Note also that as I have no use for the third parameter to the trigger method (which is the previous value of the attribute), I'm ignoring it.