perl script.pl will compile script.pl , then execute script.pl . Similarly, require Module; will compile Module.pm , then execute Module.pm .
If the compiler encounters a BEGIN block, it will execute the block as soon as the block is compiled. Keep in mind that use is a BEGIN block consisting of require and possibly a import .
For instance,
# script.pl use Foo; my $foo = Foo->new(); $foo->do();
whether:
- Compile
script.pl- Compile
use Foo; - Run
require Foo; - Run
import Foo; - Compile
my $foo = Foo->new(); - Compile
$foo->do();
- Run
script.pl- Run
my $foo = Foo->new(); - Run
$foo->do();
source share