> -----Message d'origine----- > De : Jose Nyimi [mailto:[EMAIL PROTECTED] > Envoyé : jeudi 10 mars 2005 23:03 > À : 'Santiago Hirschfeld'; beginners@perl.org > Objet : RE : Design Plugin System > > > > > -----Message d'origine----- > > De : Santiago Hirschfeld [mailto:[EMAIL PROTECTED] > > Envoyé : jeudi 10 mars 2005 21:58 > > À : beginners@perl.org > > Objet : Design Plugin System > > > > Hi everyone, > > > > I'm pretty new to perl and i'm doing a program to organize music, I'd > > like to make a plugin system for the file formats, so i can just use > > the formats y ussually have, and not include a long list of "use" in > > my main program. > > I was thinking in create modules in MyProg/AudioFormats/ with names as > > Ogg.pm and then call them automattically in my main program (which > > should check for all existing files and then create a hash containing > > the extentions the plugin module manages and the module name (like > > ('ogg' => 'Ogg', 'mp3' => 'Mp3',) ) and use some standard subs in > > every module (get_file_tag, get_know_extentions, set_file_tag). > > My idea is to call the right get_file_tag for every file, so > > MyProg::AudioFormats::Ogg::get_file_tag is called when an ogg file is > > used. And that is what i don't know how to do =) > > > > Any ideas? > > > > Sorry for the bad english and the long post. > > > > Thanks in advance. > > > > If You don't like a long list of "use Foo.pm", > you can dynamically load your module like this: > > eval "require $plugin"; > if($@){ > die qq/Couldn't load plugin "$plugin", "$@"/; > } > else{ > my $obj = $plugin->new(); > $obj->$method($args); > } > > Yes! $plugin, $obj, $method, $args can all be known only at runtime ;) > > Cheers, > José.
If you don't like the "eval" approach, here is an alternative: use UNIVERSAL::require; $plugin->require; #exception handling goes here For more info have a look to the UNIVERSAL::require module at: http://search.cpan.org/~mschwern/UNIVERSAL-exports-0.03/lib/UNIVERSAL/re quire.pm No need to install the module yourself, it comes with your Perl dist. (at least I found it in my perl5.8.6 from activestate) BR, José. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>