Once upon a time I wrote a C++ MP3 streaming server, this had a core of code, and a framework for plugins.

 These plugins worked pretty simply via 'dlopen', etc.

 Now I'm recoding the server in perl, and I'd like to have a simple means of extending the software.

 Initially I though I could have a 'Plugins::Foo' package and 'use/require' it at runtime via 'eval' however this has the problem that the package loaded thusly can't access variables in the main scope of the server, because they're defined via 'my $foo = ..'.

 Is there a simple solution to this, without using 'our'??

 Right now the best solution I've got is something along these lines: (This loads the code via an eval; with the sideeffect that the three plugin functions are present at ::main:: scope - and can access things)

$plugin = 'foo'; if ( -e $plugin . ".pm" ) { #load plugin, import it into main scope. my $fileContents = &readFile( $plugin . ".pm" ); eval( $fileContents ); if ( $@ ) { handlePluginLoadError($@); exit; } # The following three functions _MUST_ be defined # by the plugin which has been loaded. Could test # they are present via symbol table introspection # to be sure, I guess. &pluginInit( "foo" ); &pluginProcess( "bar" ); &pluginTerminae( "baz" ); # Unload those functions so that they may be # reloaded without duplication warnings undef( &pluginInit ); undef( &pluginProcess ); undef( &pluginTerminate ); }

In reply to Coding perl a plugin system? by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.