当我在Devel::NYTProf v4的输出中查找CGI program时,我在报告“源代码文件”中遇到了diagnostics.pm —按排他时间和名称排序。

首先,我不明白为什么这会出现在生产代码中。我对报告进行了更深入的研究,发现它被main::BEGIN@17调用。依次是以下行:

# spent 34µs (26+8) within main::BEGIN@15 which was called: # once (26µs+8µs) by main::RUNTIME at line 15
use strict;
# spent 34µs making 1 call to main::BEGIN@15 # spent 8µs making 1 call to strict::import

# spent 36µs (17+19) within main::BEGIN@16 which was called: # once (17µs+19µs) by main::RUNTIME at line 16
use warnings;

# spent 36µs making 1 call to main::BEGIN@16 # spent 19µs making 1 call to warnings::import

# spent 292ms (171+121) within main::BEGIN@17 which was called: # once (171ms+121ms) by main::RUNTIME at line 17
no diagnostics;
# spent 292ms making 1 call to main::BEGIN@17

# spent 135µs (27+108) within main::BEGIN@18 which was called: # once (27µs+108µs) by main::RUNTIME at line 18
use Carp qw( carp croak );

因此,这似乎是罪魁祸首。我删除了no diagnostics行, call 消失了,有效地节省了约300ms的时间。

这是perldoc use 关于no关键字的内容:



因此,这是我的实际问题:我是否假设我调用no diagnostics正确,但实际上是在unimport ed之前加载的?

no diagnostics的调用是否类似于这段代码?
BEGIN {
  require diagnostics.pm;
  diagnostics->unimport;
}

因此,仅取消导入从未导入的东西是一个坏主意,因为这实际上是首先加载它的?

最佳答案



是。其确实完全等同于

BEGIN {
  require diagnostics;
  diagnostics->unimport;
}

因此,no module命令实际上加载并编译了模块。包括执行不在BEGIN块中的中的任何代码;对于给定模块的所有依赖项都相同(对于每个使用 / 需要在内部使用)。

10-06 02:17