1Mojo::EventEmitter(3) User Contributed Perl DocumentationMojo::EventEmitter(3)
2
3
4

NAME

6       Mojo::EventEmitter - Event emitter base class
7

SYNOPSIS

9         package Cat;
10         use Mojo::Base 'Mojo::EventEmitter', -signatures;
11
12         # Emit events
13         sub poke ($self) { $self->emit(roar => 3) }
14
15         package main;
16
17         # Subscribe to events
18         my $tiger = Cat->new;
19         $tiger->on(roar => sub ($tiger, $times) { say 'RAWR!' for 1 .. $times });
20         $tiger->poke;
21

DESCRIPTION

23       Mojo::EventEmitter is a simple base class for event emitting objects.
24

EVENTS

26       Mojo::EventEmitter can emit the following events.
27
28   error
29         $e->on(error => sub ($e, $err) {...});
30
31       This is a special event for errors, it will not be emitted directly by
32       this class, but is fatal if unhandled.  Subclasses may choose to emit
33       it, but are not required to do so.
34
35         $e->on(error => sub ($e, $err) { say "This looks bad: $err" });
36

METHODS

38       Mojo::EventEmitter inherits all methods from Mojo::Base and implements
39       the following new ones.
40
41   catch
42         $e = $e->catch(sub {...});
43
44       Subscribe to "error" event.
45
46         # Longer version
47         $e->on(error => sub {...});
48
49   emit
50         $e = $e->emit('foo');
51         $e = $e->emit('foo', 123);
52
53       Emit event.
54
55   has_subscribers
56         my $bool = $e->has_subscribers('foo');
57
58       Check if event has subscribers.
59
60   on
61         my $cb = $e->on(foo => sub {...});
62
63       Subscribe to event.
64
65         $e->on(foo => sub ($e, @args) {...});
66
67   once
68         my $cb = $e->once(foo => sub {...});
69
70       Subscribe to event and unsubscribe again after it has been emitted
71       once.
72
73         $e->once(foo => sub ($e, @args) {...});
74
75   subscribers
76         my $subscribers = $e->subscribers('foo');
77
78       All subscribers for event.
79
80         # Unsubscribe last subscriber
81         $e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
82
83         # Change order of subscribers
84         @{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};
85
86   unsubscribe
87         $e = $e->unsubscribe('foo');
88         $e = $e->unsubscribe(foo => $cb);
89
90       Unsubscribe from event.
91

DEBUGGING

93       You can set the "MOJO_EVENTEMITTER_DEBUG" environment variable to get
94       some advanced diagnostics information printed to "STDERR".
95
96         MOJO_EVENTEMITTER_DEBUG=1
97

SEE ALSO

99       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
100
101
102
103perl v5.34.0                      2022-01-21             Mojo::EventEmitter(3)
Impressum