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

Discussion in 'Perl Misc' started by Tony Winslow, Dec 31, 2007.

  1. Tony Winslow

    Tony Winslow Guest

    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!
    Tony Winslow, Dec 31, 2007
    #1
    1. Advertising

  2. 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();
    }
    Joost Diepenmaat, Dec 31, 2007
    #2
    1. Advertising

  3. Tony Winslow

    John Bokma Guest

    John Bokma, Dec 31, 2007
    #3
  4. 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.
    Joost Diepenmaat, Dec 31, 2007
    #4
  5. Tony Winslow

    Tony Winslow Guest

    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!
    Tony Winslow, Dec 31, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    838
    Andrew Thompson
    Nov 16, 2005
  2. F. GEIGER
    Replies:
    9
    Views:
    1,218
    F. GEIGER
    May 3, 2004
  3. vijay
    Replies:
    8
    Views:
    703
  4. Roy
    Replies:
    6
    Views:
    610
    Roedy Green
    Jan 7, 2008
  5. Larry
    Replies:
    27
    Views:
    424
    Michele Dondi
    Jan 25, 2005
Loading...

Share This Page