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 );
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|