Ok, here is one of the more obscure methods of doing that I think. One that I would have used long ago if I had know how to. You can store your code in files as anonymous subs.
You might take a look at Perl POE for examples.
http://poe.perl.org
On the POE site there is an example job server you can pass code_refs to, which would work with this.
Then you can load your code from where ever into a hash so you can use a dispatch table.
Found this bit on some node on PM here recently.
my %dispatch_table = ( some_name_one => \&do_something, some_name_two
+=> \&do_something_else );
($dispatch_table{"$some_name"} || sub { print "no command found\n" })-
+>($my_args,@into_sub);
Here is an example of a plugin loader I'm working on for a IRC Bot.
# the code is loaded into $code from a DB in my case
# and it loops around the sub below to load all plugins.
# In your file you would have an anonymous sub routine
# like { my $times=shift; for (1...$times) { print "Hello, World!\n";
+} }
# check reference type
my $eval_code = sub { $code };
my $chk_ref=ref $eval_code;
if ($chk_ref !~ /CODE/) {
print "[load_plugins] Error loading plugin $name: Not CODE!\n";
} else {
# test code
$eval_code = eval "sub { $code }";
}
if ($@) {
chomp($@);
print "[load_plugins] Error loading plugin $name: $@\n";
} else {
# store code in a hash
$code{$name} = (
{ id => $id,
name => $name,
code => $eval_code,
},
);
print "[load_plugins] Plugin $name loaded successfully.\n";
}
|