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

T

Tony Winslow

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!
 
J

Joost Diepenmaat

Tony Winslow said:
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();
}
 
J

John Bokma

Joost Diepenmaat said:
eval "use $plugin" or die; # I prefer this over require ... import,

Heh, how about turning

die;

into something like:

die "Couldn't load plug-in: $@";
 
J

Joost Diepenmaat

John Bokma said:
Heh, how about turning

die;

into something like:

die "Couldn't load plug-in: $@";

Because I think

$ perl -e'eval "use Nothing;" or die'

Can't locate Nothing.pm in @INC (@INC contains: ..... .) at (eval 1) line 1.
BEGIN failed--compilation aborted at (eval 1) line 1.
...propagated at -e line 1.

works almost as well.

IOW, because die without arguments propagates $@ already and it's a short
example.

Joost.
 
T

Tony Winslow

Joost said:
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();
}

That is exactly what I need.
Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top