NAME
Ryu::Observable - plus ça change
SYNOPSIS
# Set initial value
my $observed = Ryu::Observable->new(123)
# and a callback for any changes
->subscribe(sub { print "New value, is now: $_\n" });
# Basic numeric increment/decrement should trigger a notification
++$observed;
# To assign a new value, use ->set_numeric or ->set_string
$observed->set_numeric(88);
DESCRIPTION
Simple monitorable variables.
METHODS
Public API, such as it is.
as_string
Returns the string representation of this value.
as_number
as_numeric
Returns the numeric representation of this value.
(this method is available as as_number
or as_numeric
, both operate the same way)
new
Instantiates with the given value.
my $observed = Ryu::Observable->new('whatever');
subscribe
Requests notifications when the value changes.
my $observed = Ryu::Observable->new('whatever')
->subscribe(sub { print "New value - $_\n" });
unsubscribe
Removes an existing callback.
my $code;
my $observed = Ryu::Observable->new('whatever')
->subscribe($code = sub { print "New value - $_\n" })
->set_string('test')
->unsubscribe($code);
set
Sets the value to the given scalar, then notifies all subscribers (regardless of whether the value has changed or not).
value
Returns the raw value.
set_numeric
set_number
Applies a new numeric value, and notifies subscribers if the value is numerically different to the previous one (or if we had no previous value).
Returns $self
.
(this method is available as set_number
or set_numeric
, both operate the same way)
set_string
Applies a new string value, and notifies subscribers if the value stringifies to a different value than the previous one (or if we had no previous value).
Returns $self
.
source
Returns a Ryu::Source, which will emit each new value until the observable is destroyed.
LVALUE METHODS
These require Sentinel to be installed.
lvalue_str
Returns a Sentinel lvalue accessor for the string value.
This can be used with refaliasing or foreach
loops to reduce typing:
for($observable->lvalue_str) {
chomp;
s/_/-/g;
}
Any attempt to retrieve or set the value will be redirected to "as_string" or "set_string" as appropriate.
lvalue_num
Returns a Sentinel lvalue accessor for the numeric value.
This can be used with refaliasing or foreach
loops to reduce typing:
for($observable->lvalue_num) {
++$_;
$_ *= 3;
}
Any attempt to retrieve or set the value will be redirected to "as_number" or "set_number" as appropriate.
finish
Mark this observable as finished.
No further events or notifications will be sent.
METHODS - Internal
Don't use these.
notify_all
Notifies all currently-subscribed callbacks with the current value.
AUTHOR
Tom Molesworth <TEAM@cpan.org>
.
LICENSE
Copyright Tom Molesworth 2011-2024. Licensed under the same terms as Perl itself.