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';
11
12         # Emit events
13         sub poke {
14           my $self = shift;
15           $self->emit(roar => 3);
16         }
17
18         package main;
19
20         # Subscribe to events
21         my $tiger = Cat->new;
22         $tiger->on(roar => sub {
23           my ($tiger, $times) = @_;
24           say 'RAWR!' for 1 .. $times;
25         });
26         $tiger->poke;
27

DESCRIPTION

29       Mojo::EventEmitter is a simple base class for event emitting objects.
30

EVENTS

32       Mojo::EventEmitter can emit the following events.
33
34   error
35         $e->on(error => sub {
36           my ($e, $err) = @_;
37           ...
38         });
39
40       This is a special event for errors, it will not be emitted directly by
41       this class, but is fatal if unhandled. Subclasses may choose to emit
42       it, but are not required to do so.
43
44         $e->on(error => sub {
45           my ($e, $err) = @_;
46           say "This looks bad: $err";
47         });
48

METHODS

50       Mojo::EventEmitter inherits all methods from Mojo::Base and implements
51       the following new ones.
52
53   catch
54         $e = $e->catch(sub {...});
55
56       Subscribe to "error" event.
57
58         # Longer version
59         $e->on(error => sub {...});
60
61   emit
62         $e = $e->emit('foo');
63         $e = $e->emit('foo', 123);
64
65       Emit event.
66
67   has_subscribers
68         my $bool = $e->has_subscribers('foo');
69
70       Check if event has subscribers.
71
72   on
73         my $cb = $e->on(foo => sub {...});
74
75       Subscribe to event.
76
77         $e->on(foo => sub {
78           my ($e, @args) = @_;
79           ...
80         });
81
82   once
83         my $cb = $e->once(foo => sub {...});
84
85       Subscribe to event and unsubscribe again after it has been emitted
86       once.
87
88         $e->once(foo => sub {
89           my ($e, @args) = @_;
90           ...
91         });
92
93   subscribers
94         my $subscribers = $e->subscribers('foo');
95
96       All subscribers for event.
97
98         # Unsubscribe last subscriber
99         $e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
100
101         # Change order of subscribers
102         @{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};
103
104   unsubscribe
105         $e = $e->unsubscribe('foo');
106         $e = $e->unsubscribe(foo => $cb);
107
108       Unsubscribe from event.
109

DEBUGGING

111       You can set the "MOJO_EVENTEMITTER_DEBUG" environment variable to get
112       some advanced diagnostics information printed to "STDERR".
113
114         MOJO_EVENTEMITTER_DEBUG=1
115

SEE ALSO

117       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
118
119
120
121perl v5.28.0                      2018-08-11             Mojo::EventEmitter(3)
Impressum