Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 executed before die?

like image 925
Eugene Yarmash Avatar asked May 11 '11 12:05

Eugene Yarmash


2 Answers

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.

like image 116
Eugene Yarmash Avatar answered Oct 14 '22 04:10

Eugene Yarmash


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.

like image 39
TLP Avatar answered Oct 14 '22 06:10

TLP