Guest User

Untitled

a guest
May 9th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. How to dynamic load plugin files in perl?
  2. Test.pm
  3. Plugins/Plugin1.pm
  4. Plugins/Plugin2.pm
  5.  
  6. sub new{
  7. #how to dynamic load plugins?
  8.  
  9. my $test=Test->new("Plugin1");
  10. $test->run ;#should call Plugin1->run
  11.  
  12. eval {
  13. require Plugin1;
  14. Plugin1->import();
  15. };
  16. if ($@) {
  17. warn "Error including Foobar: $@";
  18. }
  19.  
  20. eval {
  21. my $module_name = 'Plugin1.pm';
  22. require $module_name;
  23. $module_name =~ s/.pm//;
  24. $module_name->import();
  25. };
  26. if ($@) {
  27. # handle error here
  28. }
  29.  
  30. eval {
  31. require $plugin;
  32. }
  33. if($@) # try another one or report error or whatever ...
  34.  
  35. require "Plugins/Plugin1.pm";
  36.  
  37. use Module::PluginFinder qw( );
  38.  
  39. my $finder = Module::PluginFinder->new(
  40. search_path => 'Plugins',
  41. );
  42.  
  43. my $test = $finder->construct("Plugin1");
  44. $test->run();
  45.  
  46. sub new {
  47. my $class = shift;
  48. ...;
  49. for my $plugin (@_){
  50. if( eval "require $plugin" ){
  51. # successful
  52. ...;
  53. }else{
  54. # unsuccessful
  55. die $@;
  56. }
  57. }
  58. ...;
  59. }
  60.  
  61. sub new {
  62. my $class = shift;
  63. ...;
  64. for my $plugin (@_){
  65. eval "require $plugin" or die $@;
  66. # successful
  67. ...;
  68. }
  69. ...;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment