| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Copyright (c) 2011-17, Mitchell Cooper | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Evented::Object: a simple yet featureful base class event framework. | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # Evented::Object is based on the libuic UIC::Evented::Object: | 
| 6 |  |  |  |  |  |  | # ... which is based on Evented::Object from foxy-java IRC Bot, | 
| 7 |  |  |  |  |  |  | # ... which is based on Evented::Object from Arinity IRC Services, | 
| 8 |  |  |  |  |  |  | # ... which is based on Evented::Object from ntirc IRC Client, | 
| 9 |  |  |  |  |  |  | # ... which is based on IRC::Evented::Object from libirc IRC Library. | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # Evented::Object and its very detailed documentation can be found | 
| 12 |  |  |  |  |  |  | # in their latest versions at https://github.com/cooper/evented-object. | 
| 13 |  |  |  |  |  |  | # | 
| 14 |  |  |  |  |  |  | package Evented::Object; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 13 |  |  | 13 |  | 167076 | use warnings; | 
|  | 13 |  |  |  |  | 31 |  | 
|  | 13 |  |  |  |  | 372 |  | 
| 17 | 13 |  |  | 13 |  | 58 | use strict; | 
|  | 13 |  |  |  |  | 27 |  | 
|  | 13 |  |  |  |  | 219 |  | 
| 18 | 13 |  |  | 13 |  | 6060 | use utf8; | 
|  | 13 |  |  |  |  | 159 |  | 
|  | 13 |  |  |  |  | 65 |  | 
| 19 | 13 |  |  | 13 |  | 444 | use 5.010; | 
|  | 13 |  |  |  |  | 45 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # these must be set before loading EventFire. | 
| 22 |  |  |  |  |  |  | our ($events, $props, %monitors); | 
| 23 |  |  |  |  |  |  | BEGIN { | 
| 24 | 13 |  |  | 13 |  | 41 | $events = 'eventedObject.events'; | 
| 25 | 13 |  |  |  |  | 273 | $props  = 'eventedObject.props'; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 13 |  |  | 13 |  | 74 | use Scalar::Util qw(weaken blessed); | 
|  | 13 |  |  |  |  | 33 |  | 
|  | 13 |  |  |  |  | 1178 |  | 
| 29 | 13 |  |  | 13 |  | 3997 | use Evented::Object::EventFire; | 
|  | 13 |  |  |  |  | 35 |  | 
|  | 13 |  |  |  |  | 329 |  | 
| 30 | 13 |  |  | 13 |  | 4284 | use Evented::Object::Collection; | 
|  | 13 |  |  |  |  | 32 |  | 
|  | 13 |  |  |  |  | 15229 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # always use 2 decimals. change other packages too. | 
| 33 |  |  |  |  |  |  | our $VERSION = '5.63'; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # creates a new evented object. | 
| 36 |  |  |  |  |  |  | sub new { | 
| 37 | 15 |  |  | 15 | 1 | 210 | my ($class, %opts) = @_; | 
| 38 | 15 |  |  |  |  | 62 | bless \%opts, $class; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | ############################# | 
| 42 |  |  |  |  |  |  | ### REGISTERING CALLBACKS ### | 
| 43 |  |  |  |  |  |  | ############################# | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | # ->register_callback() | 
| 46 |  |  |  |  |  |  | # | 
| 47 |  |  |  |  |  |  | # aliases: ->register_event(), ->on() | 
| 48 |  |  |  |  |  |  | # attaches an event callback. | 
| 49 |  |  |  |  |  |  | # | 
| 50 |  |  |  |  |  |  | # $eo->register_callback(myEvent => sub { | 
| 51 |  |  |  |  |  |  | #     ... | 
| 52 |  |  |  |  |  |  | # }, 'some.callback.name', priority => 200); | 
| 53 |  |  |  |  |  |  | # | 
| 54 |  |  |  |  |  |  | sub register_callback { | 
| 55 | 35 |  |  | 35 | 1 | 356 | my ($eo, $event_name, $code, @opts_) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # if there is an odd number of options, the first is the callback name. | 
| 58 |  |  |  |  |  |  | # this also implies with_eo. | 
| 59 | 35 |  |  |  |  | 64 | my %opts; | 
| 60 | 35 | 50 |  |  |  | 110 | if (@opts_ % 2) { | 
| 61 | 0 |  |  |  |  | 0 | %opts = ( | 
| 62 |  |  |  |  |  |  | name    => shift @opts_, | 
| 63 |  |  |  |  |  |  | with_eo => 1, | 
| 64 |  |  |  |  |  |  | @opts_ | 
| 65 |  |  |  |  |  |  | ); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | else { | 
| 68 | 35 |  |  |  |  | 87 | %opts = @opts_; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # no name was provided, so we shall construct one using pure hackery. | 
| 72 |  |  |  |  |  |  | # this is one of the most criminal things I've ever done. | 
| 73 | 35 |  |  |  |  | 128 | my @caller = caller; | 
| 74 | 35 | 100 |  |  |  | 115 | if (!defined $opts{name}) { | 
| 75 | 21 |  |  |  |  | 44 | state $c    = -1; $c++; | 
|  | 21 |  |  |  |  | 36 |  | 
| 76 | 21 |  |  |  |  | 122 | $opts{name} = "$event_name:$caller[0]($caller[2],$c)"; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # determine the event store. | 
| 80 | 35 |  |  |  |  | 98 | my $event_store = _event_store($eo); | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | # before/after a callback. | 
| 83 | 35 |  | 100 |  |  | 155 | my $priority = delete $opts{priority} || 0; | 
| 84 | 35 | 100 | 100 |  |  | 186 | if (defined $opts{before} or defined $opts{after}) { | 
| 85 | 7 |  |  |  |  | 14 | $priority = 'nan'; | 
| 86 |  |  |  |  |  |  | # nan priority indicates it should be determined at a later time. | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # add the callback. | 
| 90 | 35 |  | 100 |  |  | 187 | my $callbacks = $event_store->{$event_name}{$priority} ||= []; | 
| 91 | 35 |  |  |  |  | 174 | push @$callbacks, my $cb = { | 
| 92 |  |  |  |  |  |  | %opts, | 
| 93 |  |  |  |  |  |  | code   => $code, | 
| 94 |  |  |  |  |  |  | caller => \@caller | 
| 95 |  |  |  |  |  |  | }; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # tell class monitor. | 
| 98 |  |  |  |  |  |  | _monitor_fire( | 
| 99 | 35 |  | 33 |  |  | 226 | $opts{_caller} // $caller[0], | 
| 100 |  |  |  |  |  |  | register_callback => $eo, $event_name, $cb | 
| 101 |  |  |  |  |  |  | ); | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 35 |  |  |  |  | 102 | return $cb; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | # ->register_callbacks() | 
| 107 |  |  |  |  |  |  | # | 
| 108 |  |  |  |  |  |  | # attaches several event callbacks at once. | 
| 109 |  |  |  |  |  |  | # | 
| 110 |  |  |  |  |  |  | sub register_callbacks { | 
| 111 | 0 |  |  | 0 | 1 | 0 | my $eo = shift; | 
| 112 | 0 |  |  |  |  | 0 | return map { $eo->register_callback(%$_, _caller => caller) } @_; | 
|  | 0 |  |  |  |  | 0 |  | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | ########################## | 
| 116 |  |  |  |  |  |  | ### DELETING CALLBACKS ### | 
| 117 |  |  |  |  |  |  | ########################## | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | # ->delete_callback(event_name => 'callback.name') | 
| 120 |  |  |  |  |  |  | # ->delete_event('event_name') | 
| 121 |  |  |  |  |  |  | # | 
| 122 |  |  |  |  |  |  | # deletes an event callback or all callbacks of an event. | 
| 123 |  |  |  |  |  |  | # returns a true value if any events were deleted, false otherwise. | 
| 124 |  |  |  |  |  |  | # more specifically, it returns the number of callbacks deleted. | 
| 125 |  |  |  |  |  |  | # | 
| 126 |  |  |  |  |  |  | sub delete_callback { | 
| 127 | 16 |  |  | 16 | 1 | 66 | my ($eo, $event_name, $name, $caller) = @_; | 
| 128 | 16 | 50 | 33 |  |  | 109 | my @caller      = $caller && ref $caller eq 'ARRAY' ? @$caller : caller; | 
| 129 | 16 |  |  |  |  | 39 | my $amount      = 0; | 
| 130 | 16 |  |  |  |  | 49 | my $event_store = _event_store($eo); | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # event does not have any callbacks. | 
| 133 | 16 | 50 |  |  |  | 66 | return 0 unless $event_store->{$event_name}; | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | # if no callback is specified, delete all events of this type. | 
| 136 | 16 | 100 |  |  |  | 63 | if (!$name) { | 
| 137 | 15 |  |  |  |  | 30 | $amount = scalar keys %{ $event_store->{$event_name} }; | 
|  | 15 |  |  |  |  | 38 |  | 
| 138 | 15 |  |  |  |  | 129 | delete $event_store->{$event_name}; | 
| 139 | 15 |  |  |  |  | 68 | _monitor_fire($caller[0], delete_event => $eo, $event_name); | 
| 140 | 15 |  |  |  |  | 40 | return $amount; | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # iterate through callbacks and delete matches. | 
| 144 | 1 |  |  |  |  | 2 | PRIORITY: foreach my $priority (keys %{ $event_store->{$event_name} }) { | 
|  | 1 |  |  |  |  | 5 |  | 
| 145 | 2 |  |  |  |  | 4 | my $callbacks = $event_store->{$event_name}{$priority}; | 
| 146 | 2 |  |  |  |  | 4 | my @goodbacks; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 2 |  |  |  |  | 4 | CALLBACK: foreach my $cb (@$callbacks) { | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # don't want this one. | 
| 151 | 2 | 100 | 66 |  |  | 15 | if (ref $cb ne 'HASH' || $cb->{name} eq $name) { | 
| 152 | 1 |  |  |  |  | 3 | $amount++; | 
| 153 | 1 |  |  |  |  | 4 | next CALLBACK; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 1 |  |  |  |  | 4 | push @goodbacks, $cb; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | # no callbacks left in this priority. | 
| 160 | 2 | 100 |  |  |  | 6 | if (!scalar @goodbacks) { | 
| 161 | 1 |  |  |  |  | 2 | delete $event_store->{$event_name}{$priority}; | 
| 162 | 1 |  |  |  |  | 10 | next PRIORITY; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | # keep these callbacks. | 
| 166 | 1 |  |  |  |  | 3 | @$callbacks = @goodbacks; | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 | 1 |  |  |  |  | 3 | return $amount; | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | # ->delete_all_events() | 
| 174 |  |  |  |  |  |  | # | 
| 175 |  |  |  |  |  |  | # deletes all the callbacks of EVERY event. | 
| 176 |  |  |  |  |  |  | # useful when you're done with an object to ensure any possible self-referencing | 
| 177 |  |  |  |  |  |  | # callbacks are properly destroyed for garbage collection to work. | 
| 178 |  |  |  |  |  |  | # | 
| 179 |  |  |  |  |  |  | sub delete_all_events { | 
| 180 | 15 |  |  | 15 | 1 | 45 | my ($eo, $amount) = (shift, 0); | 
| 181 | 15 | 50 |  |  |  | 50 | my $event_store   = _event_store($eo) or return; | 
| 182 | 15 | 50 |  |  |  | 76 | ref $event_store eq 'HASH'            or return; | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | # delete one-by-one. | 
| 185 |  |  |  |  |  |  | # we can't simply set an empty list because monitor events must be fired. | 
| 186 | 15 |  |  |  |  | 94 | foreach my $event_name (keys %$event_store) { | 
| 187 | 14 |  |  |  |  | 67 | $eo->delete_event($event_name); | 
| 188 | 14 |  |  |  |  | 36 | $amount++; | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | # just clear it to be safe. | 
| 192 | 15 |  |  |  |  | 51 | %$event_store = (); | 
| 193 | 15 |  |  |  |  | 49 | delete $eo->{$events}; | 
| 194 | 15 |  |  |  |  | 38 | delete $eo->{$props}; | 
| 195 |  |  |  |  |  |  |  | 
| 196 | 15 |  |  |  |  | 350 | return $amount; | 
| 197 |  |  |  |  |  |  | } | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | ######################## | 
| 200 |  |  |  |  |  |  | ### PREPARING EVENTS ### | 
| 201 |  |  |  |  |  |  | ######################## | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | # ->prepare() | 
| 204 |  |  |  |  |  |  | # | 
| 205 |  |  |  |  |  |  | # automatically guesses whether to use | 
| 206 |  |  |  |  |  |  | # ->prepare_event() or ->prepare_together(). | 
| 207 |  |  |  |  |  |  | # | 
| 208 |  |  |  |  |  |  | sub prepare { | 
| 209 | 2 |  |  | 2 | 1 | 9 | my ($eo_maybe, $eo) = $_[0]; | 
| 210 | 2 | 50 | 33 |  |  | 21 | $eo = shift if blessed $eo_maybe && $eo_maybe->isa(__PACKAGE__); | 
| 211 | 2 | 50 | 33 |  |  | 9 | if (ref $_[0] && ref $_[0] eq 'ARRAY') { | 
| 212 | 0 |  |  |  |  | 0 | return $eo->prepare_together(@_); | 
| 213 |  |  |  |  |  |  | } | 
| 214 | 2 |  |  |  |  | 7 | return $eo->prepare_event(@_); | 
| 215 |  |  |  |  |  |  | } | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | # ->prepare_event() | 
| 218 |  |  |  |  |  |  | # | 
| 219 |  |  |  |  |  |  | # prepares a single event fire by creating a callback collection. | 
| 220 |  |  |  |  |  |  | # returns the collection. | 
| 221 |  |  |  |  |  |  | # | 
| 222 |  |  |  |  |  |  | sub prepare_event { | 
| 223 | 13 |  |  | 13 | 1 | 45 | my ($eo, $event_name, @args) = @_; | 
| 224 | 13 |  |  |  |  | 58 | return $eo->prepare_together([ $event_name, @args ]); | 
| 225 |  |  |  |  |  |  | } | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | # ->prepare_together() | 
| 228 |  |  |  |  |  |  | # | 
| 229 |  |  |  |  |  |  | # prepares several events fire by creating a callback collection. | 
| 230 |  |  |  |  |  |  | # returns the collection. | 
| 231 |  |  |  |  |  |  | # | 
| 232 |  |  |  |  |  |  | sub prepare_together { | 
| 233 | 13 |  |  | 13 | 1 | 34 | my $obj; | 
| 234 | 13 |  |  |  |  | 102 | my $collection = Evented::Object::Collection->new; | 
| 235 | 13 |  |  |  |  | 39 | foreach my $set (@_) { | 
| 236 | 26 |  |  |  |  | 49 | my $eo; | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | # called with evented object. | 
| 239 | 26 | 100 |  |  |  | 110 | if (blessed $set) { | 
| 240 | 13 | 50 |  |  |  | 112 | $set->isa(__PACKAGE__) or return; | 
| 241 | 13 |  |  |  |  | 27 | $obj = $set; | 
| 242 | 13 |  |  |  |  | 36 | next; | 
| 243 |  |  |  |  |  |  | } | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | # okay, it's an array ref of | 
| 246 |  |  |  |  |  |  | # [ $eo (optional), $event_name => @args ] | 
| 247 | 13 | 50 |  |  |  | 50 | ref $set eq 'ARRAY' or next; | 
| 248 | 13 |  |  |  |  | 34 | my ($eo_maybe, $event_name, @args); | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | # was an object specified? | 
| 251 | 13 |  |  |  |  | 60 | $eo_maybe = shift @$set; | 
| 252 | 13 | 50 | 33 |  |  | 77 | if (blessed $eo_maybe && $eo_maybe->isa(__PACKAGE__)) { | 
| 253 | 0 |  |  |  |  | 0 | $eo = $eo_maybe; | 
| 254 | 0 |  |  |  |  | 0 | ($event_name, @args) = @$set; | 
| 255 |  |  |  |  |  |  | } | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | # no object; fall back to $obj. | 
| 258 |  |  |  |  |  |  | else { | 
| 259 | 13 | 50 |  |  |  | 45 | $eo = $obj or return; | 
| 260 | 13 |  |  |  |  | 44 | ($event_name, @args) = ($eo_maybe, @$set); | 
| 261 |  |  |  |  |  |  | } | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | # add to the collection. | 
| 264 | 13 |  |  |  |  | 63 | my ($callbacks, $names) = | 
| 265 |  |  |  |  |  |  | _get_callbacks($eo, $event_name, @args); | 
| 266 | 13 |  |  |  |  | 108 | $collection->push_callbacks($callbacks, $names); | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | } | 
| 269 |  |  |  |  |  |  |  | 
| 270 | 13 |  |  |  |  | 81 | return $collection; | 
| 271 |  |  |  |  |  |  | } | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | ##################### | 
| 274 |  |  |  |  |  |  | ### FIRING EVENTS ### | 
| 275 |  |  |  |  |  |  | ##################### | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | # ->fire_event() | 
| 278 |  |  |  |  |  |  | # | 
| 279 |  |  |  |  |  |  | # prepares an event and then fires it. | 
| 280 |  |  |  |  |  |  | # | 
| 281 |  |  |  |  |  |  | sub fire_event { | 
| 282 | 11 |  |  | 11 | 1 | 95 | shift->prepare_event(shift, @_)->fire(caller => [caller 1]); | 
| 283 |  |  |  |  |  |  | } | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | # ->fire_events_together() | 
| 286 |  |  |  |  |  |  | # fire_events_together() | 
| 287 |  |  |  |  |  |  | # | 
| 288 |  |  |  |  |  |  | # prepares several events and then fires them together. | 
| 289 |  |  |  |  |  |  | # | 
| 290 |  |  |  |  |  |  | sub fire_events_together { | 
| 291 | 0 |  |  | 0 | 1 | 0 | prepare_together(@_)->fire(caller => [caller 1]); | 
| 292 |  |  |  |  |  |  | } | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  | # ->fire_once() | 
| 295 |  |  |  |  |  |  | # | 
| 296 |  |  |  |  |  |  | # prepares an event, fires it, and deletes all callbacks afterward. | 
| 297 |  |  |  |  |  |  | # | 
| 298 |  |  |  |  |  |  | sub fire_once { | 
| 299 | 0 |  |  | 0 | 1 | 0 | my ($eo, $event_name, @args) = @_; | 
| 300 |  |  |  |  |  |  |  | 
| 301 |  |  |  |  |  |  | # fire with this caller. | 
| 302 | 0 |  |  |  |  | 0 | my $fire = $eo->prepare_event($event_name, @args)->fire( | 
| 303 |  |  |  |  |  |  | caller => [caller 1] | 
| 304 |  |  |  |  |  |  | ); | 
| 305 |  |  |  |  |  |  |  | 
| 306 |  |  |  |  |  |  | # delete the event. | 
| 307 | 0 |  |  |  |  | 0 | $eo->delete_event($event_name); | 
| 308 | 0 |  |  |  |  | 0 | return $fire; | 
| 309 |  |  |  |  |  |  |  | 
| 310 |  |  |  |  |  |  | } | 
| 311 |  |  |  |  |  |  |  | 
| 312 |  |  |  |  |  |  | ######################## | 
| 313 |  |  |  |  |  |  | ### LISTENER OBJECTS ### | 
| 314 |  |  |  |  |  |  | ######################## | 
| 315 |  |  |  |  |  |  |  | 
| 316 |  |  |  |  |  |  | # ->add_listener() | 
| 317 |  |  |  |  |  |  | # | 
| 318 |  |  |  |  |  |  | # adds an object as a listener of another object's events. | 
| 319 |  |  |  |  |  |  | # see "listeners" in the documentation. | 
| 320 |  |  |  |  |  |  | # | 
| 321 |  |  |  |  |  |  | sub add_listener { | 
| 322 | 3 |  |  | 3 | 1 | 24 | my ($eo, $obj, $prefix) = @_; | 
| 323 |  |  |  |  |  |  |  | 
| 324 |  |  |  |  |  |  | # find listeners list. | 
| 325 | 3 |  | 50 |  |  | 39 | my $listeners = $eo->{$props}{listeners} ||= []; | 
| 326 |  |  |  |  |  |  |  | 
| 327 |  |  |  |  |  |  | # store this listener. | 
| 328 | 3 |  |  |  |  | 10 | push @$listeners, [$prefix, $obj]; | 
| 329 |  |  |  |  |  |  |  | 
| 330 |  |  |  |  |  |  | # weaken the reference to the listener. | 
| 331 | 3 |  |  |  |  | 40 | weaken($listeners->[$#$listeners][1]); | 
| 332 |  |  |  |  |  |  |  | 
| 333 | 3 |  |  |  |  | 9 | return 1; | 
| 334 |  |  |  |  |  |  | } | 
| 335 |  |  |  |  |  |  |  | 
| 336 |  |  |  |  |  |  | # ->delete_listener() | 
| 337 |  |  |  |  |  |  | # | 
| 338 |  |  |  |  |  |  | # removes an object which was listening to another object's events. | 
| 339 |  |  |  |  |  |  | # see "listeners" in the documentation. | 
| 340 |  |  |  |  |  |  | # | 
| 341 |  |  |  |  |  |  | sub delete_listener { | 
| 342 | 0 |  |  | 0 | 1 | 0 | my ($eo, $obj) = @_; | 
| 343 | 0 | 0 |  |  |  | 0 | return 1 unless my $listeners = $eo->{$props}{listeners}; | 
| 344 |  |  |  |  |  |  | @$listeners = grep { | 
| 345 | 0 | 0 |  |  |  | 0 | ref $_->[1] eq 'ARRAY' and $_->[1] != $obj | 
|  | 0 |  |  |  |  | 0 |  | 
| 346 |  |  |  |  |  |  | } @$listeners; | 
| 347 | 0 |  |  |  |  | 0 | return 1; | 
| 348 |  |  |  |  |  |  | } | 
| 349 |  |  |  |  |  |  |  | 
| 350 |  |  |  |  |  |  | ###################### | 
| 351 |  |  |  |  |  |  | ### CLASS MONITORS ### | 
| 352 |  |  |  |  |  |  | ###################### | 
| 353 |  |  |  |  |  |  |  | 
| 354 |  |  |  |  |  |  | # for objective use $eo->monitor_events($pkg) | 
| 355 | 0 |  |  | 0 | 1 | 0 | sub monitor_events  {    add_class_monitor(reverse @_) } | 
| 356 | 0 |  |  | 0 | 1 | 0 | sub stop_monitoring { delete_class_monitor(reverse @_) } | 
| 357 |  |  |  |  |  |  |  | 
| 358 |  |  |  |  |  |  | # add_class_monitor() | 
| 359 |  |  |  |  |  |  | # | 
| 360 |  |  |  |  |  |  | # set the monitor object of a class. | 
| 361 |  |  |  |  |  |  | # | 
| 362 |  |  |  |  |  |  | # TODO: honestly class monitors need to track individual callbacks so that the | 
| 363 |  |  |  |  |  |  | # monitor is notified of all deletes of callbacks added by the class being | 
| 364 |  |  |  |  |  |  | # monitored even if the delete action was not committed by that package. | 
| 365 |  |  |  |  |  |  | # | 
| 366 |  |  |  |  |  |  | sub add_class_monitor { | 
| 367 | 0 |  |  | 0 | 0 | 0 | my ($pkg, $obj) = @_; | 
| 368 |  |  |  |  |  |  |  | 
| 369 |  |  |  |  |  |  | # ensure it's an evented object. | 
| 370 | 0 | 0 |  |  |  | 0 | return unless $obj->isa(__PACKAGE__); | 
| 371 |  |  |  |  |  |  |  | 
| 372 |  |  |  |  |  |  | # it's already in the list. | 
| 373 | 0 |  | 0 |  |  | 0 | my $m = $monitors{$pkg} ||= []; | 
| 374 | 0 | 0 |  |  |  | 0 | return if grep { $_ == $obj } @$m = grep { defined } @$m; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 375 |  |  |  |  |  |  |  | 
| 376 |  |  |  |  |  |  | # hold a weak reference to the monitor. | 
| 377 | 0 |  |  |  |  | 0 | push @$m, $obj; | 
| 378 | 0 |  |  |  |  | 0 | weaken($monitors{$pkg}[$#$m]); | 
| 379 |  |  |  |  |  |  |  | 
| 380 | 0 |  |  |  |  | 0 | return 1; | 
| 381 |  |  |  |  |  |  | } | 
| 382 |  |  |  |  |  |  |  | 
| 383 |  |  |  |  |  |  | # delete_class_monitor() | 
| 384 |  |  |  |  |  |  | # | 
| 385 |  |  |  |  |  |  | # remove a class monitor object from a class. | 
| 386 |  |  |  |  |  |  | # | 
| 387 |  |  |  |  |  |  | sub delete_class_monitor { | 
| 388 | 0 |  |  | 0 | 0 | 0 | my ($pkg, $obj) = @_; | 
| 389 | 0 | 0 |  |  |  | 0 | my $m = $monitors{$pkg} or return; | 
| 390 | 0 | 0 |  |  |  | 0 | @$m   = grep { defined && $_ != $obj } @$m; | 
|  | 0 |  |  |  |  | 0 |  | 
| 391 |  |  |  |  |  |  | } | 
| 392 |  |  |  |  |  |  |  | 
| 393 |  |  |  |  |  |  | ####################### | 
| 394 |  |  |  |  |  |  | ### CLASS FUNCTIONS ### | 
| 395 |  |  |  |  |  |  | ####################### | 
| 396 |  |  |  |  |  |  |  | 
| 397 |  |  |  |  |  |  | # safe_fire($obj, event => ...) | 
| 398 |  |  |  |  |  |  | # | 
| 399 |  |  |  |  |  |  | # checks that an object is blessed and that it is an evented object. | 
| 400 |  |  |  |  |  |  | # if so, prepares and fires an event with optional arguments. | 
| 401 |  |  |  |  |  |  | # | 
| 402 |  |  |  |  |  |  | sub safe_fire { | 
| 403 | 0 |  |  | 0 | 1 | 0 | my $obj = shift; | 
| 404 | 0 | 0 | 0 |  |  | 0 | return if !blessed $obj || !$obj->isa(__PACKAGE__); | 
| 405 | 0 |  |  |  |  | 0 | return $obj->fire_event(@_); | 
| 406 |  |  |  |  |  |  | } | 
| 407 |  |  |  |  |  |  |  | 
| 408 |  |  |  |  |  |  | ######################### | 
| 409 |  |  |  |  |  |  | ### INTERNAL ROUTINES ### | 
| 410 |  |  |  |  |  |  | ######################### | 
| 411 |  |  |  |  |  |  |  | 
| 412 |  |  |  |  |  |  | # access package storage. | 
| 413 |  |  |  |  |  |  | sub _package_store { | 
| 414 | 13 |  |  | 13 |  | 31 | my $package = shift; | 
| 415 | 13 |  |  | 13 |  | 99 | no strict 'refs'; | 
|  | 13 |  |  |  |  | 23 |  | 
|  | 13 |  |  |  |  | 7827 |  | 
| 416 | 13 |  |  |  |  | 47 | my $ref = "${package}::__EO__"; | 
| 417 | 13 | 100 |  |  |  | 104 | if (!keys %$ref) { | 
| 418 | 12 |  |  |  |  | 49 | %$ref = (); | 
| 419 |  |  |  |  |  |  | } | 
| 420 | 13 |  |  |  |  | 61 | return *$ref{HASH}; | 
| 421 |  |  |  |  |  |  | } | 
| 422 |  |  |  |  |  |  |  | 
| 423 |  |  |  |  |  |  | # fetch the event store of object or package. | 
| 424 |  |  |  |  |  |  | sub _event_store { | 
| 425 | 79 |  |  | 79 |  | 143 | my $eo    = shift; | 
| 426 | 79 | 100 | 100 |  |  | 699 | return $eo->{$events}   ||= {} if blessed $eo; | 
| 427 | 13 |  |  |  |  | 66 | my $store = _package_store($eo); | 
| 428 | 13 | 50 | 100 |  |  | 161 | return $store->{events} ||= {} if not blessed $eo; | 
| 429 |  |  |  |  |  |  | } | 
| 430 |  |  |  |  |  |  |  | 
| 431 |  |  |  |  |  |  | # fetch the property store of object or package. | 
| 432 |  |  |  |  |  |  | sub _prop_store { | 
| 433 | 0 |  |  | 0 |  | 0 | my $eo    = shift; | 
| 434 | 0 | 0 | 0 |  |  | 0 | return $eo->{$props}   ||= {} if blessed $eo; | 
| 435 | 0 |  |  |  |  | 0 | my $store = _package_store($eo); | 
| 436 | 0 | 0 | 0 |  |  | 0 | return $store->{props} ||= {} if not blessed $eo; | 
| 437 |  |  |  |  |  |  | } | 
| 438 |  |  |  |  |  |  |  | 
| 439 |  |  |  |  |  |  | # fetch a callback from its name. | 
| 440 |  |  |  |  |  |  | sub _get_callback_named { | 
| 441 | 0 |  |  | 0 |  | 0 | my ($eo, $event_name, $callback_name) = @_; | 
| 442 | 0 |  |  |  |  | 0 | foreach my $callback (@{ _get_callbacks($eo, $event_name) }) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 443 | 0 | 0 |  |  |  | 0 | return $callback if $callback->[2]{name} eq $callback_name | 
| 444 |  |  |  |  |  |  | } | 
| 445 | 0 |  |  |  |  | 0 | return; | 
| 446 |  |  |  |  |  |  | } | 
| 447 |  |  |  |  |  |  |  | 
| 448 |  |  |  |  |  |  | # fetches callbacks of an event. | 
| 449 |  |  |  |  |  |  | # internal use only. | 
| 450 |  |  |  |  |  |  | sub _get_callbacks { | 
| 451 | 13 |  |  | 13 |  | 53 | my ($eo, $event_name, @args) = @_; | 
| 452 | 13 |  |  |  |  | 28 | my (%callbacks, %callback_names); | 
| 453 |  |  |  |  |  |  |  | 
| 454 |  |  |  |  |  |  | # start out with two stores: the object and the package. | 
| 455 |  |  |  |  |  |  | my @stores = ( | 
| 456 | 13 |  |  |  |  | 99 | [ $event_name => $eo->{$events}             ], | 
| 457 |  |  |  |  |  |  | [ $event_name => _event_store(blessed $eo)  ] | 
| 458 |  |  |  |  |  |  | ); | 
| 459 |  |  |  |  |  |  |  | 
| 460 |  |  |  |  |  |  |  | 
| 461 |  |  |  |  |  |  | # if there are any listening objects, add those stores. | 
| 462 | 13 | 100 |  |  |  | 79 | if (my $listeners = $eo->{$props}{listeners}) { | 
| 463 | 3 |  |  |  |  | 8 | my @delete; | 
| 464 |  |  |  |  |  |  |  | 
| 465 | 3 |  |  |  |  | 15 | LISTENER: foreach my $i (0 .. $#$listeners) { | 
| 466 | 3 | 50 |  |  |  | 15 | my $l = $listeners->[$i] or next; | 
| 467 | 3 |  |  |  |  | 10 | my ($prefix, $lis) = @$l; | 
| 468 | 3 |  |  |  |  | 8 | my $listener_event_name = $prefix.q(.).$event_name; | 
| 469 |  |  |  |  |  |  |  | 
| 470 |  |  |  |  |  |  | # object has been deallocated by garbage disposal, | 
| 471 |  |  |  |  |  |  | # so we can delete this listener. | 
| 472 | 3 | 50 |  |  |  | 13 | if (!$lis) { | 
| 473 | 0 |  |  |  |  | 0 | push @delete, $i; | 
| 474 | 0 |  |  |  |  | 0 | next LISTENER; | 
| 475 |  |  |  |  |  |  | } | 
| 476 |  |  |  |  |  |  |  | 
| 477 |  |  |  |  |  |  |  | 
| 478 | 3 |  |  |  |  | 13 | push @stores, [ $listener_event_name => $lis->{$events} ]; | 
| 479 |  |  |  |  |  |  |  | 
| 480 |  |  |  |  |  |  | } | 
| 481 |  |  |  |  |  |  |  | 
| 482 |  |  |  |  |  |  | # delete listeners if necessary. | 
| 483 | 3 |  |  |  |  | 33 | splice @$listeners, $_, 1 foreach @delete; | 
| 484 |  |  |  |  |  |  |  | 
| 485 |  |  |  |  |  |  | } | 
| 486 |  |  |  |  |  |  |  | 
| 487 |  |  |  |  |  |  | # add callbacks from each store. | 
| 488 | 13 |  |  |  |  | 42 | foreach my $st (@stores) { | 
| 489 | 29 |  |  |  |  | 87 | my ($event_name, $event_store) = @$st; | 
| 490 | 29 | 100 |  |  |  | 151 | my $store = $event_store->{$event_name} or next; | 
| 491 | 15 |  |  |  |  | 52 | foreach my $priority (keys %$store) { | 
| 492 |  |  |  |  |  |  |  | 
| 493 |  |  |  |  |  |  | # create a group reference. | 
| 494 | 24 |  |  |  |  | 79 | my $group_id = "$eo/$event_name"; | 
| 495 | 24 |  |  |  |  | 75 | my $group    = [ $eo, $event_name, \@args, $group_id ]; | 
| 496 | 24 |  |  |  |  | 86 | weaken($group->[0]); | 
| 497 |  |  |  |  |  |  |  | 
| 498 |  |  |  |  |  |  | # add each callback set. inject callback name. | 
| 499 | 24 |  |  |  |  | 47 | foreach my $cb_ref (@{ $store->{$priority} }) { | 
|  | 24 |  |  |  |  | 57 |  | 
| 500 | 29 |  |  |  |  | 135 | my %cb = %$cb_ref; # make a copy | 
| 501 | 29 |  |  |  |  | 88 | $cb{id} = "$group_id/$cb{name}"; | 
| 502 | 29 |  |  |  |  | 103 | $callbacks{ $cb{id} } = [ $priority, $group, \%cb ]; | 
| 503 | 29 |  |  |  |  | 166 | $callback_names{$group_id}{ $cb{name} } = $cb{id}; | 
| 504 |  |  |  |  |  |  | } | 
| 505 |  |  |  |  |  |  |  | 
| 506 |  |  |  |  |  |  | } | 
| 507 |  |  |  |  |  |  | } | 
| 508 |  |  |  |  |  |  |  | 
| 509 | 13 | 50 |  |  |  | 74 | return wantarray ? (\%callbacks, \%callback_names) : \%callbacks; | 
| 510 |  |  |  |  |  |  | } | 
| 511 |  |  |  |  |  |  |  | 
| 512 |  |  |  |  |  |  | # fire a class monitor event. | 
| 513 |  |  |  |  |  |  | sub _monitor_fire { | 
| 514 | 50 |  |  | 50 |  | 146 | my ($pkg, $event_name, @args) = @_; | 
| 515 | 50 | 50 |  |  |  | 172 | my $m = $monitors{$pkg} or return; | 
| 516 | 0 |  |  |  |  | 0 | safe_fire($_, "monitor:$event_name" => @args) foreach @$m; | 
| 517 |  |  |  |  |  |  | } | 
| 518 |  |  |  |  |  |  |  | 
| 519 | 14 |  |  | 14 |  | 12724 | sub DESTROY { shift->delete_all_events } | 
| 520 |  |  |  |  |  |  |  | 
| 521 |  |  |  |  |  |  | ############### | 
| 522 |  |  |  |  |  |  | ### ALIASES ### | 
| 523 |  |  |  |  |  |  | ############### | 
| 524 |  |  |  |  |  |  |  | 
| 525 |  |  |  |  |  |  | sub register_event; | 
| 526 |  |  |  |  |  |  | sub register_events; | 
| 527 |  |  |  |  |  |  | sub delete_event; | 
| 528 |  |  |  |  |  |  |  | 
| 529 |  |  |  |  |  |  | sub on; | 
| 530 |  |  |  |  |  |  | sub del; | 
| 531 |  |  |  |  |  |  | sub fire; | 
| 532 |  |  |  |  |  |  |  | 
| 533 |  |  |  |  |  |  | BEGIN { | 
| 534 | 13 |  |  | 13 |  | 74 | *register_event     = *register_callback; | 
| 535 | 13 |  |  |  |  | 80 | *register_events    = *register_callbacks; | 
| 536 | 13 |  |  |  |  | 31 | *delete_event       = *delete_callback; | 
| 537 | 13 |  |  |  |  | 31 | *on                 = *register_callback; | 
| 538 | 13 |  |  |  |  | 30 | *del                = *delete_callback; | 
| 539 | 13 |  |  |  |  | 1786 | *fire               = *fire_event; | 
| 540 |  |  |  |  |  |  | } | 
| 541 |  |  |  |  |  |  |  | 
| 542 |  |  |  |  |  |  | 1; | 
| 543 |  |  |  |  |  |  |  | 
| 544 |  |  |  |  |  |  | =head1 NAME | 
| 545 |  |  |  |  |  |  |  | 
| 546 |  |  |  |  |  |  | B - a base class which allows you to attach callbacks to | 
| 547 |  |  |  |  |  |  | objects and then fire events on them. | 
| 548 |  |  |  |  |  |  |  | 
| 549 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 550 |  |  |  |  |  |  |  | 
| 551 |  |  |  |  |  |  | package Person; | 
| 552 |  |  |  |  |  |  |  | 
| 553 |  |  |  |  |  |  | use warnings; | 
| 554 |  |  |  |  |  |  | use strict; | 
| 555 |  |  |  |  |  |  | use 5.010; | 
| 556 |  |  |  |  |  |  | use parent 'Evented::Object'; | 
| 557 |  |  |  |  |  |  |  | 
| 558 |  |  |  |  |  |  | use Evented::Object; | 
| 559 |  |  |  |  |  |  |  | 
| 560 |  |  |  |  |  |  | # Creates a new person object. This is nothing special. | 
| 561 |  |  |  |  |  |  | # Evented::Object does not require any specific constructor to be called. | 
| 562 |  |  |  |  |  |  | sub new { | 
| 563 |  |  |  |  |  |  | my ($class, %opts) = @_; | 
| 564 |  |  |  |  |  |  | bless \%opts, $class; | 
| 565 |  |  |  |  |  |  | } | 
| 566 |  |  |  |  |  |  |  | 
| 567 |  |  |  |  |  |  | # Fires birthday event and increments age. | 
| 568 |  |  |  |  |  |  | sub have_birthday { | 
| 569 |  |  |  |  |  |  | my $person = shift; | 
| 570 |  |  |  |  |  |  | $person->fire(birthday => ++$person->{age}); | 
| 571 |  |  |  |  |  |  | } | 
| 572 |  |  |  |  |  |  |  | 
| 573 |  |  |  |  |  |  | In some other package... | 
| 574 |  |  |  |  |  |  |  | 
| 575 |  |  |  |  |  |  | package main; | 
| 576 |  |  |  |  |  |  |  | 
| 577 |  |  |  |  |  |  | # Create a person named Jake at age 19. | 
| 578 |  |  |  |  |  |  | my $jake = Person->new(name => 'Jake', age => 19); | 
| 579 |  |  |  |  |  |  |  | 
| 580 |  |  |  |  |  |  | # Add an event callback that assumes Jake is under 21. | 
| 581 |  |  |  |  |  |  | $jake->on(birthday => sub { | 
| 582 |  |  |  |  |  |  | my ($fire, $new_age) = @_; | 
| 583 |  |  |  |  |  |  | say 'not quite 21 yet...'; | 
| 584 |  |  |  |  |  |  | }, name => '21-soon'); | 
| 585 |  |  |  |  |  |  |  | 
| 586 |  |  |  |  |  |  | # Add an event callback that checks if Jake is 21 | 
| 587 |  |  |  |  |  |  | # and cancels the above callback if so. | 
| 588 |  |  |  |  |  |  | $jake->on(birthday => sub { | 
| 589 |  |  |  |  |  |  | my ($fire, $new_age) =  @_; | 
| 590 |  |  |  |  |  |  | if ($new_age == 21) { | 
| 591 |  |  |  |  |  |  | say 'time to get drunk!'; | 
| 592 |  |  |  |  |  |  | $fire->cancel('21-soon'); | 
| 593 |  |  |  |  |  |  | } | 
| 594 |  |  |  |  |  |  | }, name => 'finally-21', priority => 1); | 
| 595 |  |  |  |  |  |  |  | 
| 596 |  |  |  |  |  |  | # Jake has two birthdays. | 
| 597 |  |  |  |  |  |  |  | 
| 598 |  |  |  |  |  |  | # Jake's 20th birthday. | 
| 599 |  |  |  |  |  |  | $jake->have_birthday; | 
| 600 |  |  |  |  |  |  |  | 
| 601 |  |  |  |  |  |  | # Jake's 21st birthday. | 
| 602 |  |  |  |  |  |  | $jake->have_birthday; | 
| 603 |  |  |  |  |  |  |  | 
| 604 |  |  |  |  |  |  | # Because 21-soon has a lower priority than finally-21, | 
| 605 |  |  |  |  |  |  | # finally-21 will cancel 21-soon if Jake is 21. | 
| 606 |  |  |  |  |  |  |  | 
| 607 |  |  |  |  |  |  | # The result: | 
| 608 |  |  |  |  |  |  | # | 
| 609 |  |  |  |  |  |  | #   not quite 21 yet... | 
| 610 |  |  |  |  |  |  | #   time to get drunk! | 
| 611 |  |  |  |  |  |  |  | 
| 612 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 613 |  |  |  |  |  |  |  | 
| 614 |  |  |  |  |  |  | B | 
| 615 |  |  |  |  |  |  |  | 
| 616 |  |  |  |  |  |  | Evented::Object supplies an (obviously objective) interface to store and manage | 
| 617 |  |  |  |  |  |  | callbacks for events, fire events upon objects, and more. | 
| 618 |  |  |  |  |  |  |  | 
| 619 |  |  |  |  |  |  | Evented::Object allows you to attach event callbacks to an object | 
| 620 |  |  |  |  |  |  | (i.e., a blessed hash reference) and then fire events on that object. Event | 
| 621 |  |  |  |  |  |  | fires are much like method calls, except that there can be many responders. | 
| 622 |  |  |  |  |  |  |  | 
| 623 |  |  |  |  |  |  | Whereas many event systems involve globally unique event names, Evented::Object | 
| 624 |  |  |  |  |  |  | allows you to attach events on specific objects. The event callbacks, | 
| 625 |  |  |  |  |  |  | priority options, and other data are stored within the object itself. | 
| 626 |  |  |  |  |  |  |  | 
| 627 |  |  |  |  |  |  | =head2 Glossary | 
| 628 |  |  |  |  |  |  |  | 
| 629 |  |  |  |  |  |  | These terms are used throughout the Evented::Object documentation. | 
| 630 |  |  |  |  |  |  |  | 
| 631 |  |  |  |  |  |  | =over | 
| 632 |  |  |  |  |  |  |  | 
| 633 |  |  |  |  |  |  | =item B | 
| 634 |  |  |  |  |  |  |  | 
| 635 |  |  |  |  |  |  | This base class, providing methods for managing events. | 
| 636 |  |  |  |  |  |  |  | 
| 637 |  |  |  |  |  |  | =item B - C<$eo> | 
| 638 |  |  |  |  |  |  |  | 
| 639 |  |  |  |  |  |  | Refers to any object instance of Evented::Object or a package which inherits | 
| 640 |  |  |  |  |  |  | from it. | 
| 641 |  |  |  |  |  |  |  | 
| 642 |  |  |  |  |  |  | See L"Evented object methods">. | 
| 643 |  |  |  |  |  |  |  | 
| 644 |  |  |  |  |  |  | =item B | 
| 645 |  |  |  |  |  |  |  | 
| 646 |  |  |  |  |  |  | Event responders, known as callbacks, consist of a code reference and sometimes | 
| 647 |  |  |  |  |  |  | additional options describing how and when they should be executed. | 
| 648 |  |  |  |  |  |  |  | 
| 649 |  |  |  |  |  |  | They are executed in descending order by priority. Numerically larger priorities | 
| 650 |  |  |  |  |  |  | are called first. This allows you to place a certain callback in front of or | 
| 651 |  |  |  |  |  |  | behind another. | 
| 652 |  |  |  |  |  |  |  | 
| 653 |  |  |  |  |  |  | =item B - C<$fire> or C<$event> | 
| 654 |  |  |  |  |  |  |  | 
| 655 |  |  |  |  |  |  | An object representing an event fire. | 
| 656 |  |  |  |  |  |  |  | 
| 657 |  |  |  |  |  |  | The fire object provides methods for fetching information related to the current | 
| 658 |  |  |  |  |  |  | event fire. It also provides an interface for modifying the behavior of the | 
| 659 |  |  |  |  |  |  | remaining callbacks. | 
| 660 |  |  |  |  |  |  |  | 
| 661 |  |  |  |  |  |  | Fire objects are specific to the particular event fire, not the event. If you | 
| 662 |  |  |  |  |  |  | fire the same event twice in a row, the event object used the first time will | 
| 663 |  |  |  |  |  |  | not be the same as the second time. Therefore, all modifications made by the | 
| 664 |  |  |  |  |  |  | fire object's methods apply only to the callbacks remaining in this particular | 
| 665 |  |  |  |  |  |  | fire. For example, C<< $fire->cancel($callback) >> will only cancel the supplied | 
| 666 |  |  |  |  |  |  | callback once. | 
| 667 |  |  |  |  |  |  |  | 
| 668 |  |  |  |  |  |  | See L"Fire object methods">. | 
| 669 |  |  |  |  |  |  |  | 
| 670 |  |  |  |  |  |  | =item B - C<$col> or C<$collection> | 
| 671 |  |  |  |  |  |  |  | 
| 672 |  |  |  |  |  |  | An object representing a group of callbacks waiting to be fired. | 
| 673 |  |  |  |  |  |  |  | 
| 674 |  |  |  |  |  |  | Sometimes it is useful to prepare an event fire before actually calling it. | 
| 675 |  |  |  |  |  |  | This way, you can provide special options for the fire. Collections are returned | 
| 676 |  |  |  |  |  |  | by the 'prepare' methods. | 
| 677 |  |  |  |  |  |  |  | 
| 678 |  |  |  |  |  |  | $eo->prepare(event_name => @args)->fire(some_fire_option => $value); | 
| 679 |  |  |  |  |  |  |  | 
| 680 |  |  |  |  |  |  | See L"Collection methods">. | 
| 681 |  |  |  |  |  |  |  | 
| 682 |  |  |  |  |  |  | =item B | 
| 683 |  |  |  |  |  |  |  | 
| 684 |  |  |  |  |  |  | An evented object that receives event notifications from another evented object. | 
| 685 |  |  |  |  |  |  |  | 
| 686 |  |  |  |  |  |  | Additional evented objects can be registered as "listeners," which allows them | 
| 687 |  |  |  |  |  |  | to respond to the events of another evented object. | 
| 688 |  |  |  |  |  |  |  | 
| 689 |  |  |  |  |  |  | Consider a scenario where you have a class whose objects represent a farm. You | 
| 690 |  |  |  |  |  |  | have another class which represents a cow. You would like to use the same | 
| 691 |  |  |  |  |  |  | callback for all of the moos that occur on the farm, regardless of which cow | 
| 692 |  |  |  |  |  |  | initiated it. | 
| 693 |  |  |  |  |  |  |  | 
| 694 |  |  |  |  |  |  | Rather than attaching an event callback to every cow, you can instead make the | 
| 695 |  |  |  |  |  |  | farm a listener of the cow. Then, you can attach a single callback to your farm. | 
| 696 |  |  |  |  |  |  | If your cow's event for mooing is C, your farm's event for mooing is | 
| 697 |  |  |  |  |  |  | C. | 
| 698 |  |  |  |  |  |  |  | 
| 699 |  |  |  |  |  |  | B | 
| 700 |  |  |  |  |  |  |  | 
| 701 |  |  |  |  |  |  | When an event is fired on an object, the same fire object is used for callbacks | 
| 702 |  |  |  |  |  |  | belonging to both the evented object and its listening objects. Therefore, | 
| 703 |  |  |  |  |  |  | callback names should be unique not only to the listener object but to the | 
| 704 |  |  |  |  |  |  | object being listened on as well. | 
| 705 |  |  |  |  |  |  |  | 
| 706 |  |  |  |  |  |  | You should also note the values of the fire object: | 
| 707 |  |  |  |  |  |  |  | 
| 708 |  |  |  |  |  |  | =over | 
| 709 |  |  |  |  |  |  |  | 
| 710 |  |  |  |  |  |  | =item * | 
| 711 |  |  |  |  |  |  |  | 
| 712 |  |  |  |  |  |  | B<< $fire->event_name >>: the name of the event from the perspective of the | 
| 713 |  |  |  |  |  |  | listener; i.e. C (NOT C) | 
| 714 |  |  |  |  |  |  |  | 
| 715 |  |  |  |  |  |  | =item * | 
| 716 |  |  |  |  |  |  |  | 
| 717 |  |  |  |  |  |  | B<< $fire->object >>: the object being listened to; i.e. C<$cow> (NOT C<$farm>) | 
| 718 |  |  |  |  |  |  |  | 
| 719 |  |  |  |  |  |  | =back | 
| 720 |  |  |  |  |  |  |  | 
| 721 |  |  |  |  |  |  | This also means that stopping the event from a listener object will cancel all | 
| 722 |  |  |  |  |  |  | remaining callbacks, including those belonging to the evented object. | 
| 723 |  |  |  |  |  |  |  | 
| 724 |  |  |  |  |  |  | =back | 
| 725 |  |  |  |  |  |  |  | 
| 726 |  |  |  |  |  |  | Evented::Object's included core packages are prefixed with C. | 
| 727 |  |  |  |  |  |  | Other packages which are specifically designed for use with Evented::Object are | 
| 728 |  |  |  |  |  |  | prefixed with C. | 
| 729 |  |  |  |  |  |  |  | 
| 730 |  |  |  |  |  |  | =head1 Evented object methods | 
| 731 |  |  |  |  |  |  |  | 
| 732 |  |  |  |  |  |  | The Evented::Object package provides several convenient methods for managing an | 
| 733 |  |  |  |  |  |  | event-driven object. | 
| 734 |  |  |  |  |  |  |  | 
| 735 |  |  |  |  |  |  | =head2 Evented::Object->new() | 
| 736 |  |  |  |  |  |  |  | 
| 737 |  |  |  |  |  |  | Creates a new Evented::Object. Typically, this method is overriden by a child | 
| 738 |  |  |  |  |  |  | class of Evented::Object. | 
| 739 |  |  |  |  |  |  |  | 
| 740 |  |  |  |  |  |  | my $eo = Evented::Object->new(); | 
| 741 |  |  |  |  |  |  |  | 
| 742 |  |  |  |  |  |  | =head2 $eo->register_callback($event_name => \&callback, %options) | 
| 743 |  |  |  |  |  |  |  | 
| 744 |  |  |  |  |  |  | Attaches an event callback the object. When the specified event is fired, each | 
| 745 |  |  |  |  |  |  | of the callbacks registered using this method will be called by descending | 
| 746 |  |  |  |  |  |  | priority order (numerically higher priority numbers are called first.) | 
| 747 |  |  |  |  |  |  |  | 
| 748 |  |  |  |  |  |  | $eo->register_callback(myEvent => sub { | 
| 749 |  |  |  |  |  |  | ... | 
| 750 |  |  |  |  |  |  | }, name => 'some.callback', priority => 200); | 
| 751 |  |  |  |  |  |  |  | 
| 752 |  |  |  |  |  |  | B | 
| 753 |  |  |  |  |  |  |  | 
| 754 |  |  |  |  |  |  | =over | 
| 755 |  |  |  |  |  |  |  | 
| 756 |  |  |  |  |  |  | =item * | 
| 757 |  |  |  |  |  |  |  | 
| 758 |  |  |  |  |  |  | B: the name of the event. | 
| 759 |  |  |  |  |  |  |  | 
| 760 |  |  |  |  |  |  | =item * | 
| 761 |  |  |  |  |  |  |  | 
| 762 |  |  |  |  |  |  | B: a CODE reference to be called when the event is fired. | 
| 763 |  |  |  |  |  |  |  | 
| 764 |  |  |  |  |  |  | =item * | 
| 765 |  |  |  |  |  |  |  | 
| 766 |  |  |  |  |  |  | B: I, a hash (NOT a hash reference) of any of the below | 
| 767 |  |  |  |  |  |  | options. | 
| 768 |  |  |  |  |  |  |  | 
| 769 |  |  |  |  |  |  | =back | 
| 770 |  |  |  |  |  |  |  | 
| 771 |  |  |  |  |  |  | B<%options - event handler options> | 
| 772 |  |  |  |  |  |  |  | 
| 773 |  |  |  |  |  |  | All of these options are B, but the use of a callback name is B | 
| 774 |  |  |  |  |  |  | recommended>. | 
| 775 |  |  |  |  |  |  |  | 
| 776 |  |  |  |  |  |  | =over | 
| 777 |  |  |  |  |  |  |  | 
| 778 |  |  |  |  |  |  | =item * | 
| 779 |  |  |  |  |  |  |  | 
| 780 |  |  |  |  |  |  | B: the name of the callback being registered. must be unique to this | 
| 781 |  |  |  |  |  |  | particular event. | 
| 782 |  |  |  |  |  |  |  | 
| 783 |  |  |  |  |  |  | =item * | 
| 784 |  |  |  |  |  |  |  | 
| 785 |  |  |  |  |  |  | B: a numerical priority of the callback. | 
| 786 |  |  |  |  |  |  |  | 
| 787 |  |  |  |  |  |  | =item * | 
| 788 |  |  |  |  |  |  |  | 
| 789 |  |  |  |  |  |  | B: the name of a callback or an array reference of callback names to | 
| 790 |  |  |  |  |  |  | precede. | 
| 791 |  |  |  |  |  |  |  | 
| 792 |  |  |  |  |  |  | =item * | 
| 793 |  |  |  |  |  |  |  | 
| 794 |  |  |  |  |  |  | B: the name of a callback or an array reference of callback names to | 
| 795 |  |  |  |  |  |  | succeed. | 
| 796 |  |  |  |  |  |  |  | 
| 797 |  |  |  |  |  |  | =item * | 
| 798 |  |  |  |  |  |  |  | 
| 799 |  |  |  |  |  |  | B: any data that will be stored as C<< $fire->callback_data >> as the | 
| 800 |  |  |  |  |  |  | callback is fired. If C is a hash reference, its values can be fetched | 
| 801 |  |  |  |  |  |  | conveniently with C<< $fire->callback_data('key') >>. | 
| 802 |  |  |  |  |  |  |  | 
| 803 |  |  |  |  |  |  | =item * | 
| 804 |  |  |  |  |  |  |  | 
| 805 |  |  |  |  |  |  | B: if true, the evented object will prepended to the argument list. | 
| 806 |  |  |  |  |  |  |  | 
| 807 |  |  |  |  |  |  | =item * | 
| 808 |  |  |  |  |  |  |  | 
| 809 |  |  |  |  |  |  | B: if true, the fire object will not be prepended to the argument | 
| 810 |  |  |  |  |  |  | list. | 
| 811 |  |  |  |  |  |  |  | 
| 812 |  |  |  |  |  |  | =back | 
| 813 |  |  |  |  |  |  |  | 
| 814 |  |  |  |  |  |  | Note: the order of objects will always be C<$eo>, C<$fire>, C<@args>, regardless | 
| 815 |  |  |  |  |  |  | of omissions. By default, the argument list is C<$fire>, C<@args>. | 
| 816 |  |  |  |  |  |  |  | 
| 817 |  |  |  |  |  |  | You may have any number of C and any | 
| 818 |  |  |  |  |  |  | number of C options for any given callback. For instance, one callback | 
| 819 |  |  |  |  |  |  | may specify to be before 'b' and 'c' but after 'a'. Evented::Object will resolve | 
| 820 |  |  |  |  |  |  | these priorities to its best ability. | 
| 821 |  |  |  |  |  |  |  | 
| 822 |  |  |  |  |  |  | In the case that the priorities can not be resolved (for instance, if a callback | 
| 823 |  |  |  |  |  |  | asks to be before 'a' and after 'b' while 'b' has already asked to be before | 
| 824 |  |  |  |  |  |  | 'a'), the behavior of Evented::Object is not guaranteed to be consistent. In | 
| 825 |  |  |  |  |  |  | other words, please do your best to not request impossible priorities. | 
| 826 |  |  |  |  |  |  |  | 
| 827 |  |  |  |  |  |  | In any case, C and C options are completely ignored when a | 
| 828 |  |  |  |  |  |  | C is explicitly specified. | 
| 829 |  |  |  |  |  |  |  | 
| 830 |  |  |  |  |  |  | =head2 $eo->register_callback($event_name => \&callback, $cb_name, %options) | 
| 831 |  |  |  |  |  |  |  | 
| 832 |  |  |  |  |  |  | If the list of options is odd, it is assumed that the first element is the | 
| 833 |  |  |  |  |  |  | callback name. In this case, the C option is also automatically | 
| 834 |  |  |  |  |  |  | enabled. | 
| 835 |  |  |  |  |  |  |  | 
| 836 |  |  |  |  |  |  | $eo->register_callback(myEvent => sub { | 
| 837 |  |  |  |  |  |  | ... | 
| 838 |  |  |  |  |  |  | }, 'some.callback, priority => 200); | 
| 839 |  |  |  |  |  |  |  | 
| 840 |  |  |  |  |  |  | See the above method specification for parameters and supported options. | 
| 841 |  |  |  |  |  |  |  | 
| 842 |  |  |  |  |  |  | =head2 $eo->register_callbacks(@events) | 
| 843 |  |  |  |  |  |  |  | 
| 844 |  |  |  |  |  |  | Registers several events at once. The arguments should be a list of hash | 
| 845 |  |  |  |  |  |  | references. These references take the same options as | 
| 846 |  |  |  |  |  |  | C<< ->register_callback() >>. Returns a list of return values in the order that | 
| 847 |  |  |  |  |  |  | the events were specified. | 
| 848 |  |  |  |  |  |  |  | 
| 849 |  |  |  |  |  |  | $eo->register_callbacks( | 
| 850 |  |  |  |  |  |  | { myEvent => \&my_event_1, name => 'cb.1', priority => 200 }, | 
| 851 |  |  |  |  |  |  | { myEvent => \&my_event_2, name => 'cb.2', priority => 100 } | 
| 852 |  |  |  |  |  |  | ); | 
| 853 |  |  |  |  |  |  |  | 
| 854 |  |  |  |  |  |  | B | 
| 855 |  |  |  |  |  |  |  | 
| 856 |  |  |  |  |  |  | =over | 
| 857 |  |  |  |  |  |  |  | 
| 858 |  |  |  |  |  |  | =item * | 
| 859 |  |  |  |  |  |  |  | 
| 860 |  |  |  |  |  |  | B: an array of hash references to pass to C<< ->register_callback() >>. | 
| 861 |  |  |  |  |  |  |  | 
| 862 |  |  |  |  |  |  | =back | 
| 863 |  |  |  |  |  |  |  | 
| 864 |  |  |  |  |  |  | =head2 $eo->delete_event($event_name) | 
| 865 |  |  |  |  |  |  |  | 
| 866 |  |  |  |  |  |  | Deletes all callbacks registered for the supplied event. | 
| 867 |  |  |  |  |  |  |  | 
| 868 |  |  |  |  |  |  | Returns a true value if any events were deleted, false otherwise. | 
| 869 |  |  |  |  |  |  |  | 
| 870 |  |  |  |  |  |  | $eo->delete_event('myEvent'); | 
| 871 |  |  |  |  |  |  |  | 
| 872 |  |  |  |  |  |  | B | 
| 873 |  |  |  |  |  |  |  | 
| 874 |  |  |  |  |  |  | =over | 
| 875 |  |  |  |  |  |  |  | 
| 876 |  |  |  |  |  |  | =item * | 
| 877 |  |  |  |  |  |  |  | 
| 878 |  |  |  |  |  |  | B: the name of the event. | 
| 879 |  |  |  |  |  |  |  | 
| 880 |  |  |  |  |  |  | =back | 
| 881 |  |  |  |  |  |  |  | 
| 882 |  |  |  |  |  |  | =head2 $eo->delete_callback($event_name) | 
| 883 |  |  |  |  |  |  |  | 
| 884 |  |  |  |  |  |  | Deletes an event callback from the object with the given callback name. | 
| 885 |  |  |  |  |  |  |  | 
| 886 |  |  |  |  |  |  | Returns a true value if any events were deleted, false otherwise. | 
| 887 |  |  |  |  |  |  |  | 
| 888 |  |  |  |  |  |  | $eo->delete_callback(myEvent => 'my.callback'); | 
| 889 |  |  |  |  |  |  |  | 
| 890 |  |  |  |  |  |  | B | 
| 891 |  |  |  |  |  |  |  | 
| 892 |  |  |  |  |  |  | =over | 
| 893 |  |  |  |  |  |  |  | 
| 894 |  |  |  |  |  |  | =item * | 
| 895 |  |  |  |  |  |  |  | 
| 896 |  |  |  |  |  |  | B: the name of the event. | 
| 897 |  |  |  |  |  |  |  | 
| 898 |  |  |  |  |  |  | =item * | 
| 899 |  |  |  |  |  |  |  | 
| 900 |  |  |  |  |  |  | B: the name of the callback being removed. | 
| 901 |  |  |  |  |  |  |  | 
| 902 |  |  |  |  |  |  | =back | 
| 903 |  |  |  |  |  |  |  | 
| 904 |  |  |  |  |  |  | =head2 $eo->fire_event($event_name => @arguments) | 
| 905 |  |  |  |  |  |  |  | 
| 906 |  |  |  |  |  |  | Fires the specified event, calling each callback that was registered with | 
| 907 |  |  |  |  |  |  | C<< ->register_callback() >> in descending order of their priorities. | 
| 908 |  |  |  |  |  |  |  | 
| 909 |  |  |  |  |  |  | $eo->fire_event('some_event'); | 
| 910 |  |  |  |  |  |  |  | 
| 911 |  |  |  |  |  |  | $eo->fire_event(some_event => $some_argument, $some_other_argument); | 
| 912 |  |  |  |  |  |  |  | 
| 913 |  |  |  |  |  |  | B | 
| 914 |  |  |  |  |  |  |  | 
| 915 |  |  |  |  |  |  | =over | 
| 916 |  |  |  |  |  |  |  | 
| 917 |  |  |  |  |  |  | =item * | 
| 918 |  |  |  |  |  |  |  | 
| 919 |  |  |  |  |  |  | B: the name of the event being fired. | 
| 920 |  |  |  |  |  |  |  | 
| 921 |  |  |  |  |  |  | =item * | 
| 922 |  |  |  |  |  |  |  | 
| 923 |  |  |  |  |  |  | B: I, list of arguments to pass to event callbacks. | 
| 924 |  |  |  |  |  |  |  | 
| 925 |  |  |  |  |  |  | =back | 
| 926 |  |  |  |  |  |  |  | 
| 927 |  |  |  |  |  |  | =head2 $eo->fire_once($event_name => @arguments) | 
| 928 |  |  |  |  |  |  |  | 
| 929 |  |  |  |  |  |  | Fires the specified event, calling each callback that was registered with | 
| 930 |  |  |  |  |  |  | C<< ->register_callback() >> in descending order of their priorities. | 
| 931 |  |  |  |  |  |  |  | 
| 932 |  |  |  |  |  |  | Then, all callbacks for the event are deleted. This method is useful for | 
| 933 |  |  |  |  |  |  | situations where an event will never be fired more than once. | 
| 934 |  |  |  |  |  |  |  | 
| 935 |  |  |  |  |  |  | $eo->fire_once('some_event'); | 
| 936 |  |  |  |  |  |  | $eo->fire_event(some_event => $some_argument, $some_other_argument); | 
| 937 |  |  |  |  |  |  | # the second does nothing because the first deleted the callbacks | 
| 938 |  |  |  |  |  |  |  | 
| 939 |  |  |  |  |  |  | B | 
| 940 |  |  |  |  |  |  |  | 
| 941 |  |  |  |  |  |  | =over | 
| 942 |  |  |  |  |  |  |  | 
| 943 |  |  |  |  |  |  | =item * | 
| 944 |  |  |  |  |  |  |  | 
| 945 |  |  |  |  |  |  | B: the name of the event being fired. | 
| 946 |  |  |  |  |  |  |  | 
| 947 |  |  |  |  |  |  | =item * | 
| 948 |  |  |  |  |  |  |  | 
| 949 |  |  |  |  |  |  | B: I, list of arguments to pass to event callbacks. | 
| 950 |  |  |  |  |  |  |  | 
| 951 |  |  |  |  |  |  | =back | 
| 952 |  |  |  |  |  |  |  | 
| 953 |  |  |  |  |  |  | =head2 $eo->add_listener($other_eo, $prefix) | 
| 954 |  |  |  |  |  |  |  | 
| 955 |  |  |  |  |  |  | Makes the passed evented object a listener of this evented object. See the | 
| 956 |  |  |  |  |  |  | "listener objects" section for more information on this feature. | 
| 957 |  |  |  |  |  |  |  | 
| 958 |  |  |  |  |  |  | $cow->add_listener($farm, 'cow'); | 
| 959 |  |  |  |  |  |  |  | 
| 960 |  |  |  |  |  |  | B | 
| 961 |  |  |  |  |  |  |  | 
| 962 |  |  |  |  |  |  | =over | 
| 963 |  |  |  |  |  |  |  | 
| 964 |  |  |  |  |  |  | =item * | 
| 965 |  |  |  |  |  |  |  | 
| 966 |  |  |  |  |  |  | B: the evented object that will listen. | 
| 967 |  |  |  |  |  |  |  | 
| 968 |  |  |  |  |  |  | =item * | 
| 969 |  |  |  |  |  |  |  | 
| 970 |  |  |  |  |  |  | B: a string that event names will be prefixed with on the listener. | 
| 971 |  |  |  |  |  |  |  | 
| 972 |  |  |  |  |  |  | =back | 
| 973 |  |  |  |  |  |  |  | 
| 974 |  |  |  |  |  |  | =head2 $eo->fire_events_together(@events) | 
| 975 |  |  |  |  |  |  |  | 
| 976 |  |  |  |  |  |  | The C function can be used as a method on evented | 
| 977 |  |  |  |  |  |  | objects. See the documentation for the function in L"Procedural functions">. | 
| 978 |  |  |  |  |  |  |  | 
| 979 |  |  |  |  |  |  | =head2 $eo->delete_listener($other_eo) | 
| 980 |  |  |  |  |  |  |  | 
| 981 |  |  |  |  |  |  | Removes a listener of this evented object. See the "listener objects" section | 
| 982 |  |  |  |  |  |  | for more information on this feature. | 
| 983 |  |  |  |  |  |  |  | 
| 984 |  |  |  |  |  |  | $cow->delete_listener($farm, 'cow'); | 
| 985 |  |  |  |  |  |  |  | 
| 986 |  |  |  |  |  |  | B | 
| 987 |  |  |  |  |  |  |  | 
| 988 |  |  |  |  |  |  | =over | 
| 989 |  |  |  |  |  |  |  | 
| 990 |  |  |  |  |  |  | =item * | 
| 991 |  |  |  |  |  |  |  | 
| 992 |  |  |  |  |  |  | B: the evented object that will listen. | 
| 993 |  |  |  |  |  |  |  | 
| 994 |  |  |  |  |  |  | =item * | 
| 995 |  |  |  |  |  |  |  | 
| 996 |  |  |  |  |  |  | B: a string that event names will be prefixed with on the listener. | 
| 997 |  |  |  |  |  |  |  | 
| 998 |  |  |  |  |  |  | =back | 
| 999 |  |  |  |  |  |  |  | 
| 1000 |  |  |  |  |  |  | =head2 $eo->delete_all_events() | 
| 1001 |  |  |  |  |  |  |  | 
| 1002 |  |  |  |  |  |  | Deletes all events and all callbacks from the object. If you know that an | 
| 1003 |  |  |  |  |  |  | evented object will no longer be used in your program, by calling this method | 
| 1004 |  |  |  |  |  |  | you can be sure that no cyclical references from within callbacks will cause the | 
| 1005 |  |  |  |  |  |  | object to be leaked. | 
| 1006 |  |  |  |  |  |  |  | 
| 1007 |  |  |  |  |  |  | =head1 Preparation methods | 
| 1008 |  |  |  |  |  |  |  | 
| 1009 |  |  |  |  |  |  | Callbacks can be prepared before being fired. This is most useful for firing | 
| 1010 |  |  |  |  |  |  | events with special fire options. | 
| 1011 |  |  |  |  |  |  |  | 
| 1012 |  |  |  |  |  |  | =head2 $eo->prepare_event(event_name => @arguments) | 
| 1013 |  |  |  |  |  |  |  | 
| 1014 |  |  |  |  |  |  | Prepares a single event for firing. Returns a collection object representing the | 
| 1015 |  |  |  |  |  |  | callbacks for the event. | 
| 1016 |  |  |  |  |  |  |  | 
| 1017 |  |  |  |  |  |  | # an example using the fire option return_check. | 
| 1018 |  |  |  |  |  |  | $eo->prepare_event(some_event => @arguments)->fire('return_check'); | 
| 1019 |  |  |  |  |  |  |  | 
| 1020 |  |  |  |  |  |  | =head2 $eo->prepare_together(@events) | 
| 1021 |  |  |  |  |  |  |  | 
| 1022 |  |  |  |  |  |  | The preparatory method equivalent to C<< ->fire_events_together >>. | 
| 1023 |  |  |  |  |  |  |  | 
| 1024 |  |  |  |  |  |  | =head2 $eo->prepare(...) | 
| 1025 |  |  |  |  |  |  |  | 
| 1026 |  |  |  |  |  |  | A smart method that uses the best guess between C<< ->prepare_event >> and | 
| 1027 |  |  |  |  |  |  | C<< ->prepare_together >>. | 
| 1028 |  |  |  |  |  |  |  | 
| 1029 |  |  |  |  |  |  | # uses ->prepare_event() | 
| 1030 |  |  |  |  |  |  | $eo->prepare(some_event => @arguments); | 
| 1031 |  |  |  |  |  |  |  | 
| 1032 |  |  |  |  |  |  | # uses ->prepare_together() | 
| 1033 |  |  |  |  |  |  | $eo->prepare( | 
| 1034 |  |  |  |  |  |  | [ some_event => @arguments ], | 
| 1035 |  |  |  |  |  |  | [ some_other => @other_arg ] | 
| 1036 |  |  |  |  |  |  | ); | 
| 1037 |  |  |  |  |  |  |  | 
| 1038 |  |  |  |  |  |  | =head1 Class monitors | 
| 1039 |  |  |  |  |  |  |  | 
| 1040 |  |  |  |  |  |  | =head2 $eo->monitor_events($pkg) | 
| 1041 |  |  |  |  |  |  |  | 
| 1042 |  |  |  |  |  |  | Registers an evented object as the class monitor for a specific package. See the | 
| 1043 |  |  |  |  |  |  | section above for more details on class monitors and their purpose. | 
| 1044 |  |  |  |  |  |  |  | 
| 1045 |  |  |  |  |  |  | my $some_eo  = Evented::Object->new; | 
| 1046 |  |  |  |  |  |  | my $other_eo = Evented::Object->new; | 
| 1047 |  |  |  |  |  |  |  | 
| 1048 |  |  |  |  |  |  | $some_eo->on('monitor:register_callback', sub { | 
| 1049 |  |  |  |  |  |  | my ($event, $eo, $event_name, $cb) = @_; | 
| 1050 |  |  |  |  |  |  | # $eo         == $other_eo | 
| 1051 |  |  |  |  |  |  | # $event_name == "blah" | 
| 1052 |  |  |  |  |  |  | # $cb         == callback hash from ->register_callback() | 
| 1053 |  |  |  |  |  |  | say "Registered $$cb{name} to $eo for $event_name"; | 
| 1054 |  |  |  |  |  |  | }); | 
| 1055 |  |  |  |  |  |  |  | 
| 1056 |  |  |  |  |  |  | $some_eo->monitor_events('Some::Class'); | 
| 1057 |  |  |  |  |  |  |  | 
| 1058 |  |  |  |  |  |  | package Some::Class; | 
| 1059 |  |  |  |  |  |  | $other_eo->on(blah => sub{}); # will trigger the callback above | 
| 1060 |  |  |  |  |  |  |  | 
| 1061 |  |  |  |  |  |  | =over | 
| 1062 |  |  |  |  |  |  |  | 
| 1063 |  |  |  |  |  |  | =item * | 
| 1064 |  |  |  |  |  |  |  | 
| 1065 |  |  |  |  |  |  | B: a package whose event activity you wish to monitor. | 
| 1066 |  |  |  |  |  |  |  | 
| 1067 |  |  |  |  |  |  | =back | 
| 1068 |  |  |  |  |  |  |  | 
| 1069 |  |  |  |  |  |  | =head2 $eo->stop_monitoring($pkg) | 
| 1070 |  |  |  |  |  |  |  | 
| 1071 |  |  |  |  |  |  | Removes an evented object from its current position as a monitor for a specific | 
| 1072 |  |  |  |  |  |  | package. See the section above for more details on class monitors and their | 
| 1073 |  |  |  |  |  |  | purpose. | 
| 1074 |  |  |  |  |  |  |  | 
| 1075 |  |  |  |  |  |  | $some_eo->stop_monitoring('Some::Class'); | 
| 1076 |  |  |  |  |  |  |  | 
| 1077 |  |  |  |  |  |  | =over | 
| 1078 |  |  |  |  |  |  |  | 
| 1079 |  |  |  |  |  |  | =item * | 
| 1080 |  |  |  |  |  |  |  | 
| 1081 |  |  |  |  |  |  | B: a package whose event activity you're monitoring. | 
| 1082 |  |  |  |  |  |  |  | 
| 1083 |  |  |  |  |  |  | =back | 
| 1084 |  |  |  |  |  |  |  | 
| 1085 |  |  |  |  |  |  | =head1 Procedural functions | 
| 1086 |  |  |  |  |  |  |  | 
| 1087 |  |  |  |  |  |  | The Evented::Object package provides some functions for use. These functions | 
| 1088 |  |  |  |  |  |  | typically are associated with more than one evented object or none at all. | 
| 1089 |  |  |  |  |  |  |  | 
| 1090 |  |  |  |  |  |  | =head2 fire_events_together(@events) | 
| 1091 |  |  |  |  |  |  |  | 
| 1092 |  |  |  |  |  |  | Fires multiple events at the same time. This allows you to fire multiple similar | 
| 1093 |  |  |  |  |  |  | events on several evented objects at the same time. It essentially pretends that | 
| 1094 |  |  |  |  |  |  | the callbacks are all for the same event and all on the same object. | 
| 1095 |  |  |  |  |  |  |  | 
| 1096 |  |  |  |  |  |  | It follows priorities throughout all of the events and all of the objects, so it | 
| 1097 |  |  |  |  |  |  | is ideal for firing similar or identical events on multiple objects. | 
| 1098 |  |  |  |  |  |  |  | 
| 1099 |  |  |  |  |  |  | The same fire object is used throughout this entire routine. This means that | 
| 1100 |  |  |  |  |  |  | callback names must unique among all of these objects and events. It also means | 
| 1101 |  |  |  |  |  |  | that stopping an event from any callback will cancel all remaining callbacks, | 
| 1102 |  |  |  |  |  |  | regardless to which event or which object they belong. | 
| 1103 |  |  |  |  |  |  |  | 
| 1104 |  |  |  |  |  |  | The function takes a list of array references in the form of: | 
| 1105 |  |  |  |  |  |  | C<< [ $evented_object, event_name => @arguments ] >> | 
| 1106 |  |  |  |  |  |  |  | 
| 1107 |  |  |  |  |  |  | Evented::Object::fire_events_together( | 
| 1108 |  |  |  |  |  |  | [ $server,  user_joined_channel => $user, $channel ], | 
| 1109 |  |  |  |  |  |  | [ $channel, user_joined         => $user           ], | 
| 1110 |  |  |  |  |  |  | [ $user,    joined_channel      => $channel        ] | 
| 1111 |  |  |  |  |  |  | ); | 
| 1112 |  |  |  |  |  |  |  | 
| 1113 |  |  |  |  |  |  | C<< ->fire_events_together >> can also be used as a method | 
| 1114 |  |  |  |  |  |  | on any evented object. | 
| 1115 |  |  |  |  |  |  |  | 
| 1116 |  |  |  |  |  |  | $eo->fire_events_together( | 
| 1117 |  |  |  |  |  |  | [ some_event => @arguments ], | 
| 1118 |  |  |  |  |  |  | [ some_other => @other_arg ] | 
| 1119 |  |  |  |  |  |  | ); | 
| 1120 |  |  |  |  |  |  |  | 
| 1121 |  |  |  |  |  |  | The above example would formerly be achieved as: | 
| 1122 |  |  |  |  |  |  |  | 
| 1123 |  |  |  |  |  |  | Evented::Object::fire_events_together( | 
| 1124 |  |  |  |  |  |  | [ $eo, some_event => @arguments ], | 
| 1125 |  |  |  |  |  |  | [ $eo, some_other => @other_arg ] | 
| 1126 |  |  |  |  |  |  | ); | 
| 1127 |  |  |  |  |  |  |  | 
| 1128 |  |  |  |  |  |  | However, other evented objects may be specified even when this is used as a | 
| 1129 |  |  |  |  |  |  | method. Basically, anywhere that an object is missing will fall back to the | 
| 1130 |  |  |  |  |  |  | object on which the method was called. | 
| 1131 |  |  |  |  |  |  |  | 
| 1132 |  |  |  |  |  |  | $eo->fire_events_together( | 
| 1133 |  |  |  |  |  |  | [ $other_eo, some_event => @arguments ], | 
| 1134 |  |  |  |  |  |  | [            some_other => @other_arg ] # no object, falls back to $eo | 
| 1135 |  |  |  |  |  |  | ); | 
| 1136 |  |  |  |  |  |  |  | 
| 1137 |  |  |  |  |  |  | B | 
| 1138 |  |  |  |  |  |  |  | 
| 1139 |  |  |  |  |  |  | =over | 
| 1140 |  |  |  |  |  |  |  | 
| 1141 |  |  |  |  |  |  | =item * | 
| 1142 |  |  |  |  |  |  |  | 
| 1143 |  |  |  |  |  |  | B: an array of events in the form of | 
| 1144 |  |  |  |  |  |  | C<< [$eo, event_name => @arguments] >>. | 
| 1145 |  |  |  |  |  |  |  | 
| 1146 |  |  |  |  |  |  | =back | 
| 1147 |  |  |  |  |  |  |  | 
| 1148 |  |  |  |  |  |  | =head2 safe_fire($eo, $event_name, @args) | 
| 1149 |  |  |  |  |  |  |  | 
| 1150 |  |  |  |  |  |  | Safely fires an event. In other words, if the C<< $eo >> is not an evented | 
| 1151 |  |  |  |  |  |  | object or is not blessed at all, the call will be ignored. This eliminates the | 
| 1152 |  |  |  |  |  |  | need to use C and C<< ->isa() >> on a value for testing whether it is | 
| 1153 |  |  |  |  |  |  | an evented object. | 
| 1154 |  |  |  |  |  |  |  | 
| 1155 |  |  |  |  |  |  | Evented::Object::safe_fire($eo, myEvent => 'my argument'); | 
| 1156 |  |  |  |  |  |  |  | 
| 1157 |  |  |  |  |  |  | B | 
| 1158 |  |  |  |  |  |  |  | 
| 1159 |  |  |  |  |  |  | =over | 
| 1160 |  |  |  |  |  |  |  | 
| 1161 |  |  |  |  |  |  | =item * | 
| 1162 |  |  |  |  |  |  |  | 
| 1163 |  |  |  |  |  |  | B: the evented object. | 
| 1164 |  |  |  |  |  |  |  | 
| 1165 |  |  |  |  |  |  | =item * | 
| 1166 |  |  |  |  |  |  |  | 
| 1167 |  |  |  |  |  |  | B: the name of the event. | 
| 1168 |  |  |  |  |  |  |  | 
| 1169 |  |  |  |  |  |  | =item * | 
| 1170 |  |  |  |  |  |  |  | 
| 1171 |  |  |  |  |  |  | B: the arguments for the event fire. | 
| 1172 |  |  |  |  |  |  |  | 
| 1173 |  |  |  |  |  |  | =back | 
| 1174 |  |  |  |  |  |  |  | 
| 1175 |  |  |  |  |  |  | =head1 Collection methods | 
| 1176 |  |  |  |  |  |  |  | 
| 1177 |  |  |  |  |  |  | L are returned by the 'prepare' methods. They represent a group of | 
| 1178 |  |  |  |  |  |  | callbacks that are about to be fired. | 
| 1179 |  |  |  |  |  |  |  | 
| 1180 |  |  |  |  |  |  | =head2 $col->fire(@options) | 
| 1181 |  |  |  |  |  |  |  | 
| 1182 |  |  |  |  |  |  | Fires the pending callbacks with the specified options, if any. If the callbacks | 
| 1183 |  |  |  |  |  |  | have not yet been sorted, they are sorted before the event is fired. | 
| 1184 |  |  |  |  |  |  |  | 
| 1185 |  |  |  |  |  |  | $eo->prepare(some_event => @arguments)->fire('safe'); | 
| 1186 |  |  |  |  |  |  |  | 
| 1187 |  |  |  |  |  |  | B | 
| 1188 |  |  |  |  |  |  |  | 
| 1189 |  |  |  |  |  |  | =over | 
| 1190 |  |  |  |  |  |  |  | 
| 1191 |  |  |  |  |  |  | =item * | 
| 1192 |  |  |  |  |  |  |  | 
| 1193 |  |  |  |  |  |  | B: I, a mixture of boolean and key:value options for the | 
| 1194 |  |  |  |  |  |  | event fire. | 
| 1195 |  |  |  |  |  |  |  | 
| 1196 |  |  |  |  |  |  | =back | 
| 1197 |  |  |  |  |  |  |  | 
| 1198 |  |  |  |  |  |  | B<@options> | 
| 1199 |  |  |  |  |  |  |  | 
| 1200 |  |  |  |  |  |  | =over | 
| 1201 |  |  |  |  |  |  |  | 
| 1202 |  |  |  |  |  |  | =item * | 
| 1203 |  |  |  |  |  |  |  | 
| 1204 |  |  |  |  |  |  | B: I, use an alternate C<[caller 1]> value for the event | 
| 1205 |  |  |  |  |  |  | fire. This is typically only used internally. | 
| 1206 |  |  |  |  |  |  |  | 
| 1207 |  |  |  |  |  |  | =item * | 
| 1208 |  |  |  |  |  |  |  | 
| 1209 |  |  |  |  |  |  | B: I, if true, the event will yield that it was stopped | 
| 1210 |  |  |  |  |  |  | if any of the callbacks return a false value. Note however that if one callbacks | 
| 1211 |  |  |  |  |  |  | returns false, the rest will still be called. The fire object will only yield | 
| 1212 |  |  |  |  |  |  | stopped status after all callbacks have been called and any number of them | 
| 1213 |  |  |  |  |  |  | returned false. | 
| 1214 |  |  |  |  |  |  |  | 
| 1215 |  |  |  |  |  |  | =item * | 
| 1216 |  |  |  |  |  |  |  | 
| 1217 |  |  |  |  |  |  | B: I, wrap all callback calls in C for safety. if any of | 
| 1218 |  |  |  |  |  |  | them fail, the event will be stopped at that point with the error. | 
| 1219 |  |  |  |  |  |  |  | 
| 1220 |  |  |  |  |  |  | =item * | 
| 1221 |  |  |  |  |  |  |  | 
| 1222 |  |  |  |  |  |  | B: I, if C above is enabled, this tells the fire | 
| 1223 |  |  |  |  |  |  | to continue even if one of the callbacks fails. This could be dangerous if any | 
| 1224 |  |  |  |  |  |  | of the callbacks expected a previous callback to be done when it actually | 
| 1225 |  |  |  |  |  |  | failed. | 
| 1226 |  |  |  |  |  |  |  | 
| 1227 |  |  |  |  |  |  | =item * | 
| 1228 |  |  |  |  |  |  |  | 
| 1229 |  |  |  |  |  |  | B: I, a scalar value that can be fetched by | 
| 1230 |  |  |  |  |  |  | C<< $fire->data >> from within the callbacks. Good for data that might be useful | 
| 1231 |  |  |  |  |  |  | sometimes but not frequently enough to deserve a spot in the argument list. If | 
| 1232 |  |  |  |  |  |  | C is a hash reference, its values can be fetched conveniently with | 
| 1233 |  |  |  |  |  |  | C<< $fire->data('key') >>. | 
| 1234 |  |  |  |  |  |  |  | 
| 1235 |  |  |  |  |  |  | =back | 
| 1236 |  |  |  |  |  |  |  | 
| 1237 |  |  |  |  |  |  | =head2 $col->sort | 
| 1238 |  |  |  |  |  |  |  | 
| 1239 |  |  |  |  |  |  | Sorts the callbacks according to C, C, and C options. | 
| 1240 |  |  |  |  |  |  |  | 
| 1241 |  |  |  |  |  |  | =head1 Fire object methods | 
| 1242 |  |  |  |  |  |  |  | 
| 1243 |  |  |  |  |  |  | L"Fire objects"> are passed to all callbacks of an Evented::Object (unless the | 
| 1244 |  |  |  |  |  |  | silent parameter was specified.) Fire objects contain information about the | 
| 1245 |  |  |  |  |  |  | event itself, the callback, the caller of the event, event data, and more. | 
| 1246 |  |  |  |  |  |  |  | 
| 1247 |  |  |  |  |  |  | =head2 $fire->object | 
| 1248 |  |  |  |  |  |  |  | 
| 1249 |  |  |  |  |  |  | Returns the evented object. | 
| 1250 |  |  |  |  |  |  |  | 
| 1251 |  |  |  |  |  |  | $fire->object->delete_event('myEvent'); | 
| 1252 |  |  |  |  |  |  |  | 
| 1253 |  |  |  |  |  |  | =head2 $fire->caller | 
| 1254 |  |  |  |  |  |  |  | 
| 1255 |  |  |  |  |  |  | Returns the value of C from within the C<< ->fire() >> method. | 
| 1256 |  |  |  |  |  |  | This allows you to determine from where the event was fired. | 
| 1257 |  |  |  |  |  |  |  | 
| 1258 |  |  |  |  |  |  | my $name   = $fire->event_name; | 
| 1259 |  |  |  |  |  |  | my @caller = $fire->caller; | 
| 1260 |  |  |  |  |  |  | say "Package $caller[0] line $caller[2] called event $name"; | 
| 1261 |  |  |  |  |  |  |  | 
| 1262 |  |  |  |  |  |  | =head2 $fire->stop($reason) | 
| 1263 |  |  |  |  |  |  |  | 
| 1264 |  |  |  |  |  |  | Cancels all remaining callbacks. This stops the rest of the event firing. After | 
| 1265 |  |  |  |  |  |  | a callback calls $fire->stop, the name of that callback is stored as | 
| 1266 |  |  |  |  |  |  | C<< $fire->stopper >>. | 
| 1267 |  |  |  |  |  |  |  | 
| 1268 |  |  |  |  |  |  | If the event has already been stopped, this method returns the reason for which | 
| 1269 |  |  |  |  |  |  | the fire was stopped or "unspecified" if no reason was given. | 
| 1270 |  |  |  |  |  |  |  | 
| 1271 |  |  |  |  |  |  | # ignore messages from trolls | 
| 1272 |  |  |  |  |  |  | if ($user eq 'noah') { | 
| 1273 |  |  |  |  |  |  | # user is a troll. | 
| 1274 |  |  |  |  |  |  | # stop further callbacks. | 
| 1275 |  |  |  |  |  |  | return $fire->stop; | 
| 1276 |  |  |  |  |  |  | } | 
| 1277 |  |  |  |  |  |  |  | 
| 1278 |  |  |  |  |  |  | =over | 
| 1279 |  |  |  |  |  |  |  | 
| 1280 |  |  |  |  |  |  | =item * | 
| 1281 |  |  |  |  |  |  |  | 
| 1282 |  |  |  |  |  |  | B: I, the reason for stopping the event fire. | 
| 1283 |  |  |  |  |  |  |  | 
| 1284 |  |  |  |  |  |  | =back | 
| 1285 |  |  |  |  |  |  |  | 
| 1286 |  |  |  |  |  |  | =head2 $fire->stopper | 
| 1287 |  |  |  |  |  |  |  | 
| 1288 |  |  |  |  |  |  | Returns the callback which called C<< $fire->stop >>. | 
| 1289 |  |  |  |  |  |  |  | 
| 1290 |  |  |  |  |  |  | if ($fire->stopper) { | 
| 1291 |  |  |  |  |  |  | say 'Fire was stopped by '.$fire->stopper; | 
| 1292 |  |  |  |  |  |  | } | 
| 1293 |  |  |  |  |  |  |  | 
| 1294 |  |  |  |  |  |  | =head2 $fire->exception | 
| 1295 |  |  |  |  |  |  |  | 
| 1296 |  |  |  |  |  |  | If the event was fired with the C<< safe >> option, it is possible that an | 
| 1297 |  |  |  |  |  |  | exception occurred in one (or more if C<< fail_continue >> enabled) callbacks. | 
| 1298 |  |  |  |  |  |  | This method returns the last exception that occurred or C<< undef >> if none | 
| 1299 |  |  |  |  |  |  | did. | 
| 1300 |  |  |  |  |  |  |  | 
| 1301 |  |  |  |  |  |  | if (my $e = $fire->exception) { | 
| 1302 |  |  |  |  |  |  | say "Exception! $e"; | 
| 1303 |  |  |  |  |  |  | } | 
| 1304 |  |  |  |  |  |  |  | 
| 1305 |  |  |  |  |  |  | =head2 $fire->called($callback) | 
| 1306 |  |  |  |  |  |  |  | 
| 1307 |  |  |  |  |  |  | If no argument is supplied, returns the number of callbacks called so far, | 
| 1308 |  |  |  |  |  |  | including the current one. If a callback argument is supplied, returns whether | 
| 1309 |  |  |  |  |  |  | that particular callback has been called. | 
| 1310 |  |  |  |  |  |  |  | 
| 1311 |  |  |  |  |  |  | say $fire->called, 'callbacks have been called so far.'; | 
| 1312 |  |  |  |  |  |  |  | 
| 1313 |  |  |  |  |  |  | if ($fire->called('some.callback')) { | 
| 1314 |  |  |  |  |  |  | say 'some.callback has been called already.'; | 
| 1315 |  |  |  |  |  |  | } | 
| 1316 |  |  |  |  |  |  |  | 
| 1317 |  |  |  |  |  |  | B | 
| 1318 |  |  |  |  |  |  |  | 
| 1319 |  |  |  |  |  |  | =over | 
| 1320 |  |  |  |  |  |  |  | 
| 1321 |  |  |  |  |  |  | =item * | 
| 1322 |  |  |  |  |  |  |  | 
| 1323 |  |  |  |  |  |  | B: I, the callback being checked. | 
| 1324 |  |  |  |  |  |  |  | 
| 1325 |  |  |  |  |  |  | =back | 
| 1326 |  |  |  |  |  |  |  | 
| 1327 |  |  |  |  |  |  | =head2 $fire->pending($callback) | 
| 1328 |  |  |  |  |  |  |  | 
| 1329 |  |  |  |  |  |  | If no argument is supplied, returns the number of callbacks pending to be | 
| 1330 |  |  |  |  |  |  | called, excluding the current one. If a callback  argument is supplied, returns | 
| 1331 |  |  |  |  |  |  | whether that particular callback is pending for being called. | 
| 1332 |  |  |  |  |  |  |  | 
| 1333 |  |  |  |  |  |  | say $fire->pending, ' callbacks are left.'; | 
| 1334 |  |  |  |  |  |  |  | 
| 1335 |  |  |  |  |  |  | if ($fire->pending('some.callback')) { | 
| 1336 |  |  |  |  |  |  | say 'some.callback will be called soon (unless it gets canceled)'; | 
| 1337 |  |  |  |  |  |  | } | 
| 1338 |  |  |  |  |  |  |  | 
| 1339 |  |  |  |  |  |  | B | 
| 1340 |  |  |  |  |  |  |  | 
| 1341 |  |  |  |  |  |  | =over | 
| 1342 |  |  |  |  |  |  |  | 
| 1343 |  |  |  |  |  |  | =item * | 
| 1344 |  |  |  |  |  |  |  | 
| 1345 |  |  |  |  |  |  | B: I, the callback being checked. | 
| 1346 |  |  |  |  |  |  |  | 
| 1347 |  |  |  |  |  |  | =back | 
| 1348 |  |  |  |  |  |  |  | 
| 1349 |  |  |  |  |  |  | =head2 $fire->cancel($callback) | 
| 1350 |  |  |  |  |  |  |  | 
| 1351 |  |  |  |  |  |  | Cancels the supplied callback once. | 
| 1352 |  |  |  |  |  |  |  | 
| 1353 |  |  |  |  |  |  | if ($user eq 'noah') { | 
| 1354 |  |  |  |  |  |  | # we don't love noah! | 
| 1355 |  |  |  |  |  |  | $fire->cancel('send.hearts'); | 
| 1356 |  |  |  |  |  |  | } | 
| 1357 |  |  |  |  |  |  |  | 
| 1358 |  |  |  |  |  |  | B | 
| 1359 |  |  |  |  |  |  |  | 
| 1360 |  |  |  |  |  |  | =over | 
| 1361 |  |  |  |  |  |  |  | 
| 1362 |  |  |  |  |  |  | =item * | 
| 1363 |  |  |  |  |  |  |  | 
| 1364 |  |  |  |  |  |  | B: the callback to be cancelled. | 
| 1365 |  |  |  |  |  |  |  | 
| 1366 |  |  |  |  |  |  | =back | 
| 1367 |  |  |  |  |  |  |  | 
| 1368 |  |  |  |  |  |  | =head2 $fire->return_of($callback) | 
| 1369 |  |  |  |  |  |  |  | 
| 1370 |  |  |  |  |  |  | Returns the return value of the supplied callback. | 
| 1371 |  |  |  |  |  |  |  | 
| 1372 |  |  |  |  |  |  | if ($fire->return_of('my.callback')) { | 
| 1373 |  |  |  |  |  |  | say 'my.callback returned a true value'; | 
| 1374 |  |  |  |  |  |  | } | 
| 1375 |  |  |  |  |  |  |  | 
| 1376 |  |  |  |  |  |  | B | 
| 1377 |  |  |  |  |  |  |  | 
| 1378 |  |  |  |  |  |  | =over | 
| 1379 |  |  |  |  |  |  |  | 
| 1380 |  |  |  |  |  |  | =item * | 
| 1381 |  |  |  |  |  |  |  | 
| 1382 |  |  |  |  |  |  | B: the desired callback. | 
| 1383 |  |  |  |  |  |  |  | 
| 1384 |  |  |  |  |  |  | =back | 
| 1385 |  |  |  |  |  |  |  | 
| 1386 |  |  |  |  |  |  | =head2 $fire->last | 
| 1387 |  |  |  |  |  |  |  | 
| 1388 |  |  |  |  |  |  | Returns the most recent previous callback called. | 
| 1389 |  |  |  |  |  |  | This is also useful for determining which callback was the last to be called. | 
| 1390 |  |  |  |  |  |  |  | 
| 1391 |  |  |  |  |  |  | say $fire->last, ' was called before this one.'; | 
| 1392 |  |  |  |  |  |  |  | 
| 1393 |  |  |  |  |  |  | my $fire = $eo->fire_event('myEvent'); | 
| 1394 |  |  |  |  |  |  | say $fire->last, ' was the last callback called.'; | 
| 1395 |  |  |  |  |  |  |  | 
| 1396 |  |  |  |  |  |  | =head2 $fire->last_return | 
| 1397 |  |  |  |  |  |  |  | 
| 1398 |  |  |  |  |  |  | Returns the last callback's return value. | 
| 1399 |  |  |  |  |  |  |  | 
| 1400 |  |  |  |  |  |  | if ($fire->last_return) { | 
| 1401 |  |  |  |  |  |  | say 'the callback before this one returned a true value.'; | 
| 1402 |  |  |  |  |  |  | } | 
| 1403 |  |  |  |  |  |  | else { | 
| 1404 |  |  |  |  |  |  | die 'the last callback returned a false value.'; | 
| 1405 |  |  |  |  |  |  | } | 
| 1406 |  |  |  |  |  |  |  | 
| 1407 |  |  |  |  |  |  | =head2 $fire->event_name | 
| 1408 |  |  |  |  |  |  |  | 
| 1409 |  |  |  |  |  |  | Returns the name of the event. | 
| 1410 |  |  |  |  |  |  |  | 
| 1411 |  |  |  |  |  |  | say 'the event being fired is ', $fire->event_name; | 
| 1412 |  |  |  |  |  |  |  | 
| 1413 |  |  |  |  |  |  | =head2 $fire->callback_name | 
| 1414 |  |  |  |  |  |  |  | 
| 1415 |  |  |  |  |  |  | Returns the name of the current callback. | 
| 1416 |  |  |  |  |  |  |  | 
| 1417 |  |  |  |  |  |  | say 'the current callback being called is ', $fire->callback_name; | 
| 1418 |  |  |  |  |  |  |  | 
| 1419 |  |  |  |  |  |  | =head2 $fire->callback_priority | 
| 1420 |  |  |  |  |  |  |  | 
| 1421 |  |  |  |  |  |  | Returns the priority of the current callback. | 
| 1422 |  |  |  |  |  |  |  | 
| 1423 |  |  |  |  |  |  | say 'the priority of the current callback is ', $fire->callback_priority; | 
| 1424 |  |  |  |  |  |  |  | 
| 1425 |  |  |  |  |  |  | =head2 $fire->callback_data($key) | 
| 1426 |  |  |  |  |  |  |  | 
| 1427 |  |  |  |  |  |  | Returns the data supplied to the callback when it was registered, if any. If the | 
| 1428 |  |  |  |  |  |  | data is a hash reference, an optional key parameter can specify a which value to | 
| 1429 |  |  |  |  |  |  | fetch. | 
| 1430 |  |  |  |  |  |  |  | 
| 1431 |  |  |  |  |  |  | say 'my data is ', $fire->callback_data; | 
| 1432 |  |  |  |  |  |  | say 'my name is ', $fire->callback_data('name'); | 
| 1433 |  |  |  |  |  |  |  | 
| 1434 |  |  |  |  |  |  | B | 
| 1435 |  |  |  |  |  |  |  | 
| 1436 |  |  |  |  |  |  | =over | 
| 1437 |  |  |  |  |  |  |  | 
| 1438 |  |  |  |  |  |  | =item * | 
| 1439 |  |  |  |  |  |  |  | 
| 1440 |  |  |  |  |  |  | B: I, a key to fetch a value if the data registered was a hash. | 
| 1441 |  |  |  |  |  |  |  | 
| 1442 |  |  |  |  |  |  | =back | 
| 1443 |  |  |  |  |  |  |  | 
| 1444 |  |  |  |  |  |  | =head2 $fire->data($key) | 
| 1445 |  |  |  |  |  |  |  | 
| 1446 |  |  |  |  |  |  | Returns the data supplied to the collection when it was fired, if any. If the | 
| 1447 |  |  |  |  |  |  | data is a hash reference, an optional key parameter can specify a which value to | 
| 1448 |  |  |  |  |  |  | fetch. | 
| 1449 |  |  |  |  |  |  |  | 
| 1450 |  |  |  |  |  |  | say 'fire data is ', $fire->data; | 
| 1451 |  |  |  |  |  |  | say 'fire time was ', $fire->data('time'); | 
| 1452 |  |  |  |  |  |  |  | 
| 1453 |  |  |  |  |  |  | B | 
| 1454 |  |  |  |  |  |  |  | 
| 1455 |  |  |  |  |  |  | =over | 
| 1456 |  |  |  |  |  |  |  | 
| 1457 |  |  |  |  |  |  | =item * | 
| 1458 |  |  |  |  |  |  |  | 
| 1459 |  |  |  |  |  |  | B: I, a key to fetch a value if the data registered was a hash. | 
| 1460 |  |  |  |  |  |  |  | 
| 1461 |  |  |  |  |  |  | =back | 
| 1462 |  |  |  |  |  |  |  | 
| 1463 |  |  |  |  |  |  | =head1 Aliases | 
| 1464 |  |  |  |  |  |  |  | 
| 1465 |  |  |  |  |  |  | A number of aliases exist for convenience, but some of the names are rather | 
| 1466 |  |  |  |  |  |  | broad. For that reason, they are only recommended for use when you are sure that | 
| 1467 |  |  |  |  |  |  | other subclassing will not interfere. | 
| 1468 |  |  |  |  |  |  |  | 
| 1469 |  |  |  |  |  |  | =head2 $eo->on(...) | 
| 1470 |  |  |  |  |  |  |  | 
| 1471 |  |  |  |  |  |  | Alias for C<< $eo->register_callback() >>. | 
| 1472 |  |  |  |  |  |  |  | 
| 1473 |  |  |  |  |  |  | =head2 $eo->del(...) | 
| 1474 |  |  |  |  |  |  |  | 
| 1475 |  |  |  |  |  |  | If one argument provided, alias for C<< $eo->delete_event >>. | 
| 1476 |  |  |  |  |  |  |  | 
| 1477 |  |  |  |  |  |  | If two arguments provided, alias for C<< $eo->delete_callback >>. | 
| 1478 |  |  |  |  |  |  |  | 
| 1479 |  |  |  |  |  |  | =head2 $eo->fire(...) | 
| 1480 |  |  |  |  |  |  |  | 
| 1481 |  |  |  |  |  |  | Alias for C<< $eo->fire_event() >>. | 
| 1482 |  |  |  |  |  |  |  | 
| 1483 |  |  |  |  |  |  | =head2 $eo->register_event(...) | 
| 1484 |  |  |  |  |  |  |  | 
| 1485 |  |  |  |  |  |  | Alias for C<< $eo->register_callback() >>. | 
| 1486 |  |  |  |  |  |  |  | 
| 1487 |  |  |  |  |  |  | =head2 $eo->register_events(...) | 
| 1488 |  |  |  |  |  |  |  | 
| 1489 |  |  |  |  |  |  | Alias for C<< $eo->register_callbacks() >>. | 
| 1490 |  |  |  |  |  |  |  | 
| 1491 |  |  |  |  |  |  | =head2 $fire->eo | 
| 1492 |  |  |  |  |  |  |  | 
| 1493 |  |  |  |  |  |  | Alias for C<< $fire->object >>. | 
| 1494 |  |  |  |  |  |  |  | 
| 1495 |  |  |  |  |  |  | =head1 ADVANCED FEATURES | 
| 1496 |  |  |  |  |  |  |  | 
| 1497 |  |  |  |  |  |  | =head2 Registering callbacks to package names | 
| 1498 |  |  |  |  |  |  |  | 
| 1499 |  |  |  |  |  |  | The methods C<< ->register_callback() >>, C<< ->delete_event() >>, | 
| 1500 |  |  |  |  |  |  | C<< ->delete_callback >>, etc. can be called in the form of | 
| 1501 |  |  |  |  |  |  | C<< MyClass->method() >>. Evented::Object will store these callbacks in the | 
| 1502 |  |  |  |  |  |  | package's symbol table. | 
| 1503 |  |  |  |  |  |  |  | 
| 1504 |  |  |  |  |  |  | Any object of this class will borrow these callbacks from the class. They will | 
| 1505 |  |  |  |  |  |  | be incorporated into the callback collection as though they were registered | 
| 1506 |  |  |  |  |  |  | directly on the object. | 
| 1507 |  |  |  |  |  |  |  | 
| 1508 |  |  |  |  |  |  | Note that events cannot be fired on a class, only on evented objects. | 
| 1509 |  |  |  |  |  |  |  | 
| 1510 |  |  |  |  |  |  | Note that if an evented object is blessed to a subclass of a class with | 
| 1511 |  |  |  |  |  |  | callbacks registered to it, the object will NOT inherit the callbacks associated | 
| 1512 |  |  |  |  |  |  | with the parent class. Callbacks registered to classes ONLY apply to objects | 
| 1513 |  |  |  |  |  |  | directly blessed to the class. | 
| 1514 |  |  |  |  |  |  |  | 
| 1515 |  |  |  |  |  |  | =head2 Class monitors | 
| 1516 |  |  |  |  |  |  |  | 
| 1517 |  |  |  |  |  |  | An evented object can be registered as a "monitor" of a specific class/package. | 
| 1518 |  |  |  |  |  |  |  | 
| 1519 |  |  |  |  |  |  | I event callbacks that are added from that class to I evented object | 
| 1520 |  |  |  |  |  |  | of I type will trigger an event on the monitor object. | 
| 1521 |  |  |  |  |  |  |  | 
| 1522 |  |  |  |  |  |  | An example scenario of when this might be useful is an evented object for | 
| 1523 |  |  |  |  |  |  | debugging all events being registered by a certain package. It would log all of | 
| 1524 |  |  |  |  |  |  | them, making it easier to find a problem. | 
| 1525 |  |  |  |  |  |  |  | 
| 1526 |  |  |  |  |  |  | =head1 AUTHOR | 
| 1527 |  |  |  |  |  |  |  | 
| 1528 |  |  |  |  |  |  | L | 
| 1529 |  |  |  |  |  |  |  | 
| 1530 |  |  |  |  |  |  | Copyright E 2011-2017. Released under BSD license. | 
| 1531 |  |  |  |  |  |  |  | 
| 1532 |  |  |  |  |  |  | Comments, complaints, and recommendations are accepted. Bugs may be reported on | 
| 1533 |  |  |  |  |  |  | L |