问题
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