Why is a 'use' statement executed first in a BEGIN block?
When I execute the following code, I get Can't locate SomePackage.pm in @INC ...
.
BEGIN {
die;
use SomePackage;
}
Why is use
exe开发者_运维问答cuted before die
?
use SomePackage
is exactly equivalent to
BEGIN { require SomePackage; SomePackage->import }
A BEGIN
code block is executed as soon as possible, that is, the moment it is completely defined. The second BEGIN
(which is implied by use
) is completely defined first, and is thus executed first.
From http://perldoc.perl.org/functions/use.html
Because use takes effect at compile time, it doesn't respect the ordinary flow control of the code being compiled.
精彩评论