2010-03-22 08:33:44 +01:00
|
|
|
# File: Registerable.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Provides functionality to register and execute one or more subroutines.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
package PBot::Registerable;
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2020-02-14 22:32:12 +01:00
|
|
|
sub new {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($proto, %conf) = @_;
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $conf{pbot};
|
|
|
|
$self->{pbot} = $conf{pbot};
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
2020-02-14 22:32:12 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
$self->{handlers} = [];
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub execute_all {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
foreach my $func (@{$self->{handlers}}) {
|
|
|
|
my $result = &{$func->{subref}}(@_);
|
|
|
|
return $result if defined $result;
|
|
|
|
}
|
|
|
|
return undef;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub execute {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $ref = shift;
|
|
|
|
Carp::croak("Missing reference parameter to Registerable::execute") if not defined $ref;
|
|
|
|
foreach my $func (@{$self->{handlers}}) {
|
|
|
|
if ($ref == $func || $ref == $func->{subref}) { return &{$func->{subref}}(@_); }
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
return undef;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub register {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $subref) = @_;
|
|
|
|
Carp::croak("Must pass subroutine reference to register()") if not defined $subref;
|
|
|
|
my $ref = {subref => $subref};
|
|
|
|
push @{$self->{handlers}}, $ref;
|
|
|
|
return $ref;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-30 05:49:24 +02:00
|
|
|
sub register_front {
|
|
|
|
my ($self, $subref) = @_;
|
|
|
|
Carp::croak("Must pass subroutine reference to register_front()") if not defined $subref;
|
|
|
|
my $ref = {subref => $subref};
|
|
|
|
unshift @{$self->{handlers}}, $ref;
|
|
|
|
return $ref;
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub unregister {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $ref) = @_;
|
|
|
|
Carp::croak("Must pass reference to unregister()") if not defined $ref;
|
|
|
|
@{$self->{handlers}} = grep { $_ != $ref } @{$self->{handlers}};
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 16:26:29 +01:00
|
|
|
sub unregister_all {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->{handlers} = [];
|
2020-02-15 16:26:29 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
1;
|