3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-16 17:09:33 +01:00
pbot/lib/PBot/Core/Class.pm

51 lines
1.4 KiB
Perl
Raw Normal View History

2021-06-19 06:23:34 +02: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-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-11 00:00:22 +02:00
# SPDX-License-Identifier: MIT
2021-07-21 07:44:51 +02:00
package PBot::Core::Class;
2021-06-19 06:23:34 +02:00
use PBot::Imports;
my %import_opts;
sub import($package, %opts) {
if (%opts) {
# set import options for package
$import_opts{$package} = \%opts;
}
}
sub new($class, %args) {
2021-06-19 06:23:34 +02:00
# ensure class was passed a PBot instance
if (not exists $args{pbot}) {
2020-02-15 23:38:32 +01:00
my ($package, $filename, $line) = caller(0);
my (undef, undef, undef, $subroutine) = caller(1);
Carp::croak("Missing pbot reference to $class, created by $subroutine at $filename:$line");
2020-02-15 23:38:32 +01:00
}
# create class instance
my $self = bless { pbot => $args{pbot} }, $class;
2021-06-19 06:23:34 +02: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-19 06:23:34 +02:00
$self->initialize(%args);
2020-02-15 23:38:32 +01:00
return $self;
}
sub initialize {
# ensure class has an initialize() subroutine
2020-02-15 23:38:32 +01:00
my ($package, $filename, $line) = caller(0);
my (undef, undef, undef, $subroutine) = caller(1);
Carp::croak("Missing initialize subroutine in $subroutine at $filename:$line");
}
1;