如果我的代码中没有诊断,为什么 Perl 会编译diagnostics.pm? [英] Why does Perl compile diagnostics.pm if I have no diagnostics in my code?

查看:48
本文介绍了如果我的代码中没有诊断,为什么 Perl 会编译diagnostics.pm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 对 no 关键字:

<块引用>

有一个相应的 no 声明可以取消导入含义通过使用导入,即调用 unimport Module LIST 而不是进口.它的行为就像 import 对 VERSION 所做的一样,省略或空的 LIST,或者没有找到 unimport 方法.

无整数;没有严格的参考";没有警告;

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

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

BEGIN {需要诊断.pm;诊断->取消导入;}

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

解决方案

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

是的.它确实完全相当于

BEGIN {需要诊断;诊断->取消导入;}

所以 no module 命令实际上加载和编译模块;包括执行不在任何sub、BEGIN块等中的代码;对于给定模块的所有依赖项(对于每个 use/require 内部)都相同.

When I was looking at the output of Devel::NYTProf v4 for a CGI program, I came across the diagnostics.pm in the report Source Code Files — ordered by exclusive time then name.

First I didn't understand why that would be in the production code. I dug deeper through the report and found out that it was called by main::BEGIN@17. That, in turn, is the following line:

# 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 );

So this seems to be the culprit. I removed the no diagnostics line, and the call was gone, effectively saving about 300ms of time.

Here's what perldoc use says about the no keyword:

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;

So here's my actual question: Am I correct in assuming that if I call no diagnostics, it is actually loaded before it is unimported?

Is the call to no diagnostics similar to this piece of code?

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

In consequence, is it a bad idea to just unimport stuff that has never been imported, because that actually loads it first?

解决方案

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

Yes. Its indeed full equivalent to

BEGIN {
  require diagnostics;
  diagnostics->unimport;
}

So no module command actually loads and compiles the module; including executing the code which is not in any sub, in BEGIN block etc; same for all dependencies of given module(for every use/ require inside).

这篇关于如果我的代码中没有诊断,为什么 Perl 会编译diagnostics.pm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆