Usually you control access to an object or attribute from the outside.
my $foo_bar_lock = Coro::Semaphore->new();
my $foo = Foo->new();
{
my $guard = $foo_bar_lock->guard;
}
{
my $guard = $foo_bar_lock->guard;
}
But it can also be done from the inside out.
has bar_lock => (
reader => '_get_bar_lock',
default => sub { Coro::Semaphore->new() },
);
has bar => (
reader => '_get_bar',
writer => '_set_bar',
builder => '_build_bar',
lazy => 1,
);
sub _build_bar { ... }
sub get_bar {
my $self = shift;
my $guard = $self->_get_bar_lock->guard;
return $self->_get_bar();
}
sub set_bar {
my $self = shift;
my $guard = $self->_get_bar_lock->guard;
return $self->_set_bar(@_);
}
(If you prefer to use only one get-set accessor instead of separate get and set accessors, use accessorinstead of readerand writer.)
source
share