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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.