2010-03-22 08:33:44 +01:00
|
|
|
# File: Registerable.pm
|
|
|
|
#
|
|
|
|
# Purpose: Provides functionality to register and execute one or more subroutines.
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::Registerable;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2020-02-14 22:32:12 +01:00
|
|
|
sub new {
|
2021-06-19 06:23:34 +02:00
|
|
|
my ($class, %args) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = bless {}, $class;
|
2021-06-19 06:23:34 +02:00
|
|
|
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $args{pbot};
|
|
|
|
$self->{pbot} = delete $args{pbot};
|
|
|
|
$self->initialize(%args);
|
2020-02-15 23:38:32 +01:00
|
|
|
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;
|