[PerlChina] 求助如何动态加载模块, dynamic load Module

rorot yang.liana at gmail.com
Sun Dec 2 06:46:43 PST 2007


诸位好,

感谢各位上次指导我的关于HTML::DOM,
Mozilla::Mechanize的诸多问题。甚至于jzhang提出的Class::DBI模块,我觉得很好用。正用在了目前的模块中。

这次我碰到的问题是: 如何在我自己编写的package里,动态加载别的模块?
具体是这样的。在我的主模块里是MVC(Module, View, Control)结构,其View输出分至少3种,或HTML, 或XML,
或Console Text. 这三种输出,分别由3个不同的View模块实现。

问题是:当我要输出是,只会调用一种输出模式。而具体要用哪个输出模块,根据运行时参数决定。我不想把三种输出模式都包进来,而是想到了具体该用哪个模块时,再加载。
如何解决呢?

我尝试了使用autouse的方式:

use autouse 'Foo::View::HTML';
use autouse 'Foo::View::XML';
use autouse 'Foo::View::Console';

sub output {
      my $self = shift;
      my $view_arg = shift;

      if ($view_arg eq "HTML")
      {
           my $view = Foo::View::HTML->new;
           $view->output("something");
      }

      ...
}


但是运行直接报错,说是找不到HTML模块。。。。
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/china-pm/attachments/20071202/9a37c683/attachment.html 


More information about the China-pm mailing list