如果我的代码中没有诊断,为什么Perl会编译diagnostics.pm?

时间:2012-11-26 14:34:06

标签: perl devel-nytprof

当我查看Devel::NYTProfCGI program v4输出时,我在报告源代码文件中遇到了diagnostics.pm - 按排序时间排序然后命名

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行,呼叫消失了,有效地节省了大约300毫秒的时间。

以下是perldoc use关于no关键字的说明:

  

有一个相应的没有声明无关紧要的含义   通过使用导入,即它调用unimport Module LIST而不是   进口。它的行为与VERSION的导入一样,省略或   空LIST,或没有找到任何不重要的方法。

no integer;
no strict 'refs';
no warnings;

所以这是我的实际问题:我是否正确认为如果我致电no diagnostics,它实际上是在unimport之前加载的?

no diagnostics的调用是否类似于这段代码?

BEGIN {
  require diagnostics.pm;
  diagnostics->unimport;
}

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

1 个答案:

答案 0 :(得分:22)

  

我是否正确地假设如果我调用no diagnostics,它实际上是在未导入之前加载的?

是。它确实完全相当于

BEGIN {
  require diagnostics;
  diagnostics->unimport;
}

所以no module命令实际上加载并编译模块;包括在BEGIN块等中执行不在任何 sub 中的代码;同样适用于给定模块的所有依赖关系(对于每个使用 / 需要)。