Why does Perl compile diagnostics.pm if I have no diagnostics in my code?

99封情书 提交于 2019-12-05 11:58:18

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/13566980/why-does-perl-compile-diagnostics-pm-if-i-have-no-diagnostics-in-my-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!