需要时加载模块
如果需要在运行时加载整个模块,则使用
require
. 但要导入,需要额外的代码。以下是示例:
## this function is almost the same
## as "use My::Module qw( :something )"
sub load_big_module_at_runtime {
## load module in runtime
require My::Module;
## do import explicty if you need it
My::Module->import( ':something' );
}
使用模块功能时加载模块
你也可以
use
autouse
仅在使用模块功能时加载模块。例如:
## will load module when you call O_EXCL()
use autouse Fcntl => qw( O_EXCL() );
仅在使用时加载函数
也有
SelfLoader
模块,它允许您仅在需要时加载单个函数。看一看
AutoLoader
做了几乎相同事情的模块。
我还建议阅读以下共同赞助的食谱:
Perl Cookbook
.