blob: 4a3235249487d48c6d7f54def14c9e8af4a1574c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package Term::Filter::Callback;
use Moose;
# ABSTRACT: Simple callback-based wrapper for L<Term::Filter>
with 'Term::Filter';
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
=attr callbacks
=cut
has callbacks => (
is => 'ro',
isa => 'HashRef[CodeRef]',
default => sub { {} },
);
sub _callback {
my $self = shift;
my ($event, @args) = @_;
my $callback = $self->callbacks->{$event};
return unless $callback;
return $self->$callback(@args);
}
sub _has_callback {
my $self = shift;
my ($event) = @_;
return exists $self->callbacks->{$event};
}
for my $method (qw(setup cleanup munge_input munge_output
read read_error winch)) {
__PACKAGE__->meta->add_around_method_modifier(
$method => sub {
my $orig = shift;
my $self = shift;
if ($self->_has_callback($method)) {
return $self->_callback($method, @_);
}
else {
return $self->$orig(@_);
}
},
);
}
__PACKAGE__->meta->make_immutable;
no Moose;
1;
|