简体   繁体   English

如何在Perl中实现延迟模块加载?

[英]How do I implement lazy module loading in Perl?

How can I implement lazy module loading in Perl? 如何在Perl中实现延迟模块加载?

I've seen similar things in python and implementation is somewhat simpler, but in Perl I think this would be a bit harder. 我已经在python中看到了类似的事情,并且实现起来有些简单,但是在Perl中,我认为这会有点困难。

Load module when you want 需要时加载模块

If you need to load whole module at runtime you use require . 如果您需要在运行时加载整个模块,请使用require But for importing you'll require additional code. 但是对于导入,您将需要其他代码。 Here is example: 这是示例:

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

Load module when its functions are used 使用功能时加载模块

You can also use autouse to load module only when its function is used. 您也可以仅在使用模块功能时使用autouse加载模块。 For example: 例如:

## will load module when you call O_EXCL()
use autouse Fcntl => qw( O_EXCL() );

Load function only when it's used 仅在使用时加载功能

There is also SelfLoader module, which allows you to load single functions only when you need it. 还有一个SelfLoader模块,它允许您仅在需要时才加载单个功能。 Take a look at AutoLoader module which doing nearly the same thing. 看一下AutoLoader模块,它执行几乎相同的操作。

I also recommend to read coresponding recipes from Perl Cookbook . 我还建议您阅读Perl Cookbook的相关食谱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM