且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态加载Perl模块

更新时间:2023-12-04 22:07:52

MooseX :: Object ::可插拔


I am trying to make an extensible system whereby I can code up a new module to be a handler. I want the program to automatically load any new .pm file that is put into the Handlers directory and conforms to a Moose::Role interface.

I'm wondering whether there is a Perl module or a more Moose sanctioned way to do this automatically? Here is what I have built so far, but it seems a little verbose and there has got to be a simpler way to do it.

handler.pl contains:

    #!/usr/bin/perl
            use Handler;
    use Data::Dumper;

    my $base_handler = Handler->new();
    $base_handler->load_modules('SysG/Handler');
    print Dumper($base_handler);

Handler.pm contains:

    package Handler;
    use Moose;

    has 'handlers' => ( traits => ['Array'], handles => { add_handler => 'push' } );

    sub load_modules {
        my ($self,$dir) = @_;

        push(@INC, $dir);

        my @modules = find_modules_to_load($dir);
        eval { 
            # Note that this sort is important. The processing order will be critically important.
            # The sort implies the sort order
            foreach my $module ( sort @modules) {
                (my $file = $module) =~ s|::|/|g;
                print "About to load $file.pm for module $module\n" ;
                require $file . '.pm';
                $module->import();
                my $obj = $module->new();
                $self->add_handler($obj);
                1;
            }
        } or do {
            my $error = $@;
            print "Error loading modules: $error" if $error;
        };

    }

    sub find_modules_to_load {
        my ($dir) = @_;
        my @files = glob("$dir/*.pm");
        my $namespace = $dir;
        $namespace =~ s/\//::/g;

        # Get the leaf name and add the System::Module namespace to it
        my @modules = map { s/.*\/(.*).pm//g;  "${namespace}::$1"; } @files;
        die "ERROR: No classifier modules found in $dir\n" unless @modules;
        return @modules;
    }

    1;

Then I have made a directory called SysG/Handler and added two .pm files which ordinarily will conform to a Moose::Role (as if to define an interface that must be adhered too).

The SysG::Handler::0001_HandleX.pm stub contains:

package SysG::Handler::0001_HandleX;
use Moose;
1;

The SysG::Handler::0002_HandleX.pm stub contains:

package SysG::Handler::0002_HandleY;
use Moose;
1;

Put all this together and the Data::Dumper result is:

$VAR1 = bless( {
             'handlers' => [
                             bless( {}, 'SysG::Handler::0001_HandleX' ),
                             bless( {}, 'SysG::Handler::0002_HandleY' )
                           ]
           }, 'Handler' );

So, now I repeat my original question: There must be a simpler way, or a module or a Moose way to automatically load any modules in a specific directory.

Any Moose experts able to help out here?

MooseX::Object::Pluggable