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

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

am 31.12.2007 06:20:20 von 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!

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

am 31.12.2007 13:49:14 von Joost Diepenmaat

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

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

am 31.12.2007 16:25:51 von John Bokma

Joost Diepenmaat wrote:

> 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: $@";

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1. html

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

am 31.12.2007 16:51:03 von Joost Diepenmaat

John Bokma writes:

> Joost Diepenmaat wrote:
>
>> 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: $@";
>

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.

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

am 31.12.2007 17:31:17 von Tony Winslow

Joost Diepenmaat wrote:
> Tony Winslow 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();
> }
>

That is exactly what I need.
Thank you!