You probably want a normal method:
package Stock;
use Moose;
has 'quantity' => ( is => 'rw', );
has 'price' => ( is => 'rw', );
sub value {
my $self = shift;
return $self->quantity * $self->price;
}
Alternatively, hook into the setters for quantity
and price
and have them update the value
whenever a new value is set.
Advantage of hooks: The value is cached, which is good when the calculation is expensive (not the case here).
Advantage of a simple method: Easier to implement.