aboutsummaryrefslogtreecommitdiff
path: root/Plugin.pm
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-21 21:29:17 +1200
committerDavid Phillips <david@sighup.nz>2018-09-21 21:32:28 +1200
commit0475f0bb2fa7ee2f211c67f8df68b014c7fbd213 (patch)
tree5d40897f5508f6c681d93145506d1e4fa0ebd53a /Plugin.pm
parentf2812c477b230a1407381bfbbb393119c7280076 (diff)
downloadidalius-0475f0bb2fa7ee2f211c67f8df68b014c7fbd213.tar.xz
Add runtime module {,un}loading
Diffstat (limited to 'Plugin.pm')
-rw-r--r--Plugin.pm34
1 files changed, 34 insertions, 0 deletions
diff --git a/Plugin.pm b/Plugin.pm
new file mode 100644
index 0000000..4aba6d2
--- /dev/null
+++ b/Plugin.pm
@@ -0,0 +1,34 @@
+package Plugin;
+
+use strict;
+use warnings;
+
+my $unload_callback;
+
+sub load_plugin {
+ my ($logger, $config, $module) = @_;
+ (my $path = $module) =~ s,::,/,g;
+
+ return "$module is already loaded, no changes made" if grep {$_ eq $module} @{$config->{active_plugins}};
+
+ eval {
+ require $path . ".pm";
+ push @{$config->{active_plugins}}, $module;
+ } or do {
+ chomp $@;
+ $logger->($@);
+ return "Cannot load $module: $!";
+ };
+ return undef;
+}
+
+sub unload_plugin {
+ my ($logger, $config, $module) = @_;
+
+ return "$module is not loaded, no changes made" unless grep {$_ eq $module} @{$config->{active_plugins}};
+
+ my @new_plugins = grep {$_ ne $module} @{$config->{active_plugins}};
+ $config->{active_plugins} = \@new_plugins;
+ return undef;
+}
+1;