Sledge::PluginLoader

nipotan on 2006-11-17T07:59:02


It's cool.

But, maybe is fault.

The plugins will be exported via Sledge::PluginLoader.

the implementation is like this:

package Sledge::PluginLoader;

use strict; use warnings; use Carp qw(croak); use UNIVERSAL::require;

sub import { my $class = shift; my $pkg = caller;

for my $name (@_) { my $plugin = "Sledge::Plugin::$name"; $plugin->require or croak $!;

unless ($plugin->can('add_methods') && $plugin->can('register_hooks')) { croak "$plugin is an old Sledge Plugin. use it directly"; }

my @method_info = $plugin->add_methods; for (my $i = 0; $i < @method_info; $i += 2) { no strict 'refs'; my $method_name = $method_info[$i]; *{"$pkg\::$method_name"} = $method_info[$i + 1]; }

my @hook_info = $plugin->resister_hooks; for (my $i = 0; $i < @hook_info; $i += 2) { $pkg->register_hook($hook_info[$i] => $hook_info[$i + 1]) } } }

1;


I didn't try it. It's no guarantee :P


and this code lost the backward compatibility.
any idea?


core?

tokuhirom on 2006-11-17T08:15:04

Index: lib/Sledge/Pages/Base.pm
====================================================== =============
--- lib/Sledge/Pages/Base.pm    (revision 2)
+++ lib/Sledge/Pages/Base.pm    (working copy)
@@ -29,6 +29,7 @@

use File::Spec;
use URI;
+use UNIVERSAL::require;

use Sledge::Exceptions;
use Sledge::FillInForm;
@@ -238,6 +239,31 @@
     return $url;
}

+sub load_plugins {
+    my ($class, @plugins) = @_;
+
+    for my $name (@plugins) {
+        my $plugin = "Sledge::Plugin::$name";
+        $plugin->require or croak $!;
+
+        unless ($plugin->can('add_methods') && $plugin->can('register_hooks')) {
+            croak "$plugin is an old Sledge Plugin. use it directly";
+        }
+
+        my @method_info = $plugin->add_methods;
+        for (my $i = 0; $i < @method_info; $i += 2) {
+            no strict 'refs';
+            my $method_name = $method_info[$i];
+            *{"$pkg\::$method_name"} = $method_info[$i + 1];
+        }
+
+        my @hook_info = $plugin->resister_hooks;
+        for (my $i = 0; $i < @hook_info; $i += 2) {
+            $pkg->register_hook($hook_info[$i] => $hook_info[$i + 1])
+        }
+    }
+}
+
sub _destroy_me {
     my $self = shift;
     # paranoia: guard against cyclic reference

Re:core?

nipotan on 2006-11-17T08:21:04

Yes, it is. it will be implemented into Sledge core is better than using loader.