3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-16 08:59:34 +01:00
pbot/lib/PBot/Core/Utils/LoadModules.pm

57 lines
1.5 KiB
Perl
Raw Normal View History

2021-07-22 14:36:46 +02:00
# File: LoadModules.pm
#
2021-07-22 14:36:46 +02:00
# Purpose: Loads all Perl modules in a given directory, nonrecursively
# (i.e. at one depth level).
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
2021-07-24 04:22:25 +02:00
package PBot::Core::Utils::LoadModules;
use PBot::Imports;
use File::Basename;
require Exporter;
our @ISA = qw/Exporter/;
2021-07-22 14:36:46 +02:00
our @EXPORT = qw/load_modules/;
sub load_modules($self, $base) {
my $base_path = join '/', split '::', $base;
foreach my $inc_path (@INC) {
if (-d "$inc_path/$base_path") {
2021-07-22 14:36:46 +02:00
my @modules = glob "$inc_path/$base_path/*.pm";
2021-07-22 14:36:46 +02:00
foreach my $module (sort @modules) {
$self->{pbot}->{refresher}->{refresher}->refresh_module($module);
2021-07-22 14:36:46 +02:00
my $name = basename $module;
$name =~ s/\.pm$//;
$self->{pbot}->{logger}->log(" $name\n");
eval {
my $class = $base . '::' . $name;
require "$module";
$class->import(quiet => 1);
2021-07-22 14:36:46 +02:00
$self->{modules}->{$name} = $class->new(pbot => $self->{pbot});
$self->{pbot}->{refresher}->{refresher}->update_cache($module);
};
2021-07-22 14:36:46 +02:00
# error loading a module
if (my $exception = $@) {
2021-07-22 14:36:46 +02:00
$self->{pbot}->{logger}->log("Error loading $module: $exception");
exit;
}
}
2021-07-22 14:36:46 +02:00
# modules loaded successfully
return 1;
}
}
2021-07-22 14:36:46 +02:00
# no module found
return 0;
}
1;