2021-06-18 21:23:34 -07:00
|
|
|
# File: Class.pm
|
|
|
|
#
|
|
|
|
# Purpose: Base class for PBot classes. This prevents each PBot class from
|
|
|
|
# needing to define the new() constructor and other boilerplate.
|
|
|
|
#
|
2023-02-20 21:31:52 -08:00
|
|
|
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-10 15:00:22 -07:00
|
|
|
# SPDX-License-Identifier: MIT
|
2020-02-08 11:04:13 -08:00
|
|
|
|
2021-07-20 22:44:51 -07:00
|
|
|
package PBot::Core::Class;
|
2020-02-08 11:04:13 -08:00
|
|
|
|
2021-06-18 21:23:34 -07:00
|
|
|
use PBot::Imports;
|
2021-06-05 13:20:03 -07:00
|
|
|
|
2021-07-27 12:01:42 -07:00
|
|
|
my %import_opts;
|
2021-07-23 07:24:30 -07:00
|
|
|
|
2023-04-13 21:04:12 -07:00
|
|
|
sub import($package, %opts) {
|
2021-07-27 12:01:42 -07:00
|
|
|
if (%opts) {
|
|
|
|
# set import options for package
|
|
|
|
$import_opts{$package} = \%opts;
|
|
|
|
}
|
2021-07-23 07:24:30 -07:00
|
|
|
}
|
|
|
|
|
2023-04-13 21:04:12 -07:00
|
|
|
sub new($class, %args) {
|
2021-06-18 21:23:34 -07:00
|
|
|
# ensure class was passed a PBot instance
|
|
|
|
if (not exists $args{pbot}) {
|
2020-02-15 14:38:32 -08:00
|
|
|
my ($package, $filename, $line) = caller(0);
|
|
|
|
my (undef, undef, undef, $subroutine) = caller(1);
|
2021-07-27 12:01:42 -07:00
|
|
|
Carp::croak("Missing pbot reference to $class, created by $subroutine at $filename:$line");
|
2020-02-15 14:38:32 -08:00
|
|
|
}
|
|
|
|
|
2021-07-27 12:01:42 -07:00
|
|
|
# create class instance
|
2021-07-20 21:38:07 -07:00
|
|
|
my $self = bless { pbot => $args{pbot} }, $class;
|
2021-06-18 21:23:34 -07:00
|
|
|
|
2021-07-27 12:01:42 -07:00
|
|
|
# log class initialization unless quieted
|
|
|
|
unless (exists $import_opts{$class} and $import_opts{$class}{quiet}) {
|
|
|
|
$self->{pbot}->{logger}->log("Initializing $class\n")
|
|
|
|
}
|
|
|
|
|
2021-06-18 21:23:34 -07:00
|
|
|
$self->initialize(%args);
|
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
return $self;
|
2020-02-08 11:04:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
2021-07-27 12:01:42 -07:00
|
|
|
# ensure class has an initialize() subroutine
|
2020-02-15 14:38:32 -08:00
|
|
|
my ($package, $filename, $line) = caller(0);
|
|
|
|
my (undef, undef, undef, $subroutine) = caller(1);
|
|
|
|
Carp::croak("Missing initialize subroutine in $subroutine at $filename:$line");
|
2020-02-08 11:04:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|