Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

nipotan (7414)

nipotan
  (email not shown publicly)
http://blog.livedoor.jp/nipotan/

Journal of nipotan (7414)

Friday November 17, 2006
02:59 AM

Sledge::PluginLoader

http://use.perl.org/~ikebe/journal/31589
It's cool.

But, maybe http://use.perl.org/~tokuhirom/journal/31638 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?