gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:42:32 25 4
gpt4 key购买 nike

当我查看 Devel::NYTProf 的输出时v4 为 CGI program ,我遇到了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关键词:

There's a corresponding no declaration that unimports meanings imported by use, i.e., it calls unimport Module LIST instead of import. It behaves just as import does with VERSION, an omitted or empty LIST, or no unimport method being found.

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


所以这是我的实际问题: 我是否正确假设如果我调用 no diagnostics ,实际上是在unimport之前加载的编?

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

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

最佳答案

Am I correct in assuming that if I call no diagnostics, it is actually loaded before it is unimported?



是的。它确实完全等同于
BEGIN {
require diagnostics;
diagnostics->unimport;
}

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

关于perl - 如果我的代码中没有诊断信息,为什么 Perl 会编译 diagnostics.pm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13566980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com