in reply to Similar plugins run serially

Warning!! What follows is just my curiosity in trying to solve the problem. It works as far as I have tested it, and that is exactly as far as you can see from the simple code that follows. Critisisms invited.

Using this main program

#! perl -sw use vars qw/@scanners/; use strict; use constant PLUGINS => 'c:/test/plugins/*'; BEGIN { @scanners = glob PLUGINS; for(@scanners) { require $_; warn $@ if $@; my ($module) = m!.*/([^\.]+)\Q.pm\E$!; $_ = eval{ "$module"->new() }; warn "Attempt to load: $module failed\n" if ( !$_ or $@ ); } @scanners = grep{ $_ } @scanners; } # call the 'scan' routine in each module, and # print out the results from the array reference returned. print @{&{$_}}, $/ for @scanners;

And these two modules

package method1; sub new{ my $class = shift; return bless \&scan, $class; } sub scan{ my @results = qw/foo bar baz/; #!do scan; return \@results; } 1;

method2.pm

package method2; sub new{ my $class = shift; return bless \&scan, $class; } sub scan{ my @results = qw/the quick brown fox/; #!do scan; return \@results; } 1;

I get these results.

C:\test>209857 foobarbaz thequickbrownfox C:\test>

which seems to indicate that the method works. However, whilst I cannot see anything wrong with the mechanism, it's the first time I tried anything like this so I am fully expecting to be told either that there is a much simpler way of doing this, or that there is something fundementally unsound in what I have done.


Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re^2: Similar plugins run serially
by Aristotle (Chancellor) on Nov 02, 2002 at 07:33 UTC
    You're regexing with \.pm$, but you're globbing for *. Doesn't sound like something I'd do. Seems a lot more natural to me do to:
    opendir my $dir, PLUGINS; @scanners = map (-f $_ ? /^(.*)\.pm$/i : ()), readdir $dir; closedir $dir; # .. require "$_.pm";
    That said, it seems like you're trying way too hard. The following is untested and may need some tweaking, but you get the idea.
    { local @INC = PLUGINS; opendir my $dir, PLUGINS; eval qq(@{[ map -f && ? /^(.*)\.pm$/ && "use \Q$1\E;", readdir $di +r ]}); closedir $dir; }

    Update: had forgotten to actually put the plugins path into @INC.

    Makeshifts last the longest.