Saving memory and compile time

孤人 提交于 2019-12-23 20:21:42

问题


Is there any way to save memory and compile time in perl using modules? For example not load all of the unneccessary, unused subs?

Or it is a good way if I split my subs to many different pm files, and then I load only neccessary modules? For example:

#!/usr/bin/perl -w

sub mysub1() {
    use MySubsGroup1;
}
sub mysub2() {
    use MySubsGroup2;
}

This solution use less memory and get less compile time? Or what is the best practice to load only neccessary functions?


回答1:


From perldoc autouse

autouse - postpone load of modules until a function is used

If the module Module is already loaded, then the declaration

use autouse 'Module' => qw(func1 func2($;$));

is equivalent to

use Module qw(func1 func2);




回答2:


use always runs in compile time. But require doesn't. So you can use require Module; just before any function call.

Of course, the module will be loaded only the first time require is executed (and stay loaded).

Mind that use also calls import on the loaded module and you may want to do that too. use Module qw(f1 f2) is a compile-time version of require Module; Module->import(qw(f1 f3)).



来源:https://stackoverflow.com/questions/30026270/saving-memory-and-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!