Re: is there any "hello-world" demo for plugin-based application?



Tony Winslow <tonywinslow1986@xxxxxxxxx> writes:

We're building a document management system which supports multiple
types of documents. Thus, the editors for different documents vary
from one another. We would like to build the system in a plugin-based
style so that we or the users of it can add new document types to it
as needed. However, we don't know exactly how to implement it in a
plugin-based flavor. So we want to find some very simple and
easy-to-learn examples. Could anybody help? Thank you!

Plugins are generally implemented using normal perl modules implementing
some interface that works for your system. This probably means it's
easiest to implement using OO modules since that gives you inheritance
of base methods and a standardized way of referencing your plugins for
free.

here's a very simple example:

# in file MyPlugin.pm

package MyPlugin;

sub new {
return bless { name => "MyPlugin plugin" }, shift;
}

sub print_hello {
my ($self) = @_;
print "Hello from ",$self->{name},"\n";
}

1;

# in your main program:

# get this list from configuration
# or scan a predefined directory...
my @plugins = qw(MyPlugin);

foreach my $plugin (@plugins) {
eval "use $plugin" or die; # I prefer this over require ... import, YMMV
$plugin->new()->print_hello();
}

.