http://qs321.pair.com?node_id=219808

skx has asked for the wisdom of the Perl Monks concerning the following question:

 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 ); }