From 0475f0bb2fa7ee2f211c67f8df68b014c7fbd213 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Fri, 21 Sep 2018 21:29:17 +1200 Subject: Add runtime module {,un}loading --- Plugin.pm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Plugin.pm (limited to 'Plugin.pm') 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; -- cgit v1.1