This is a result of the constant folding inside of Perl. This can be demonstrated with the following example from haarg
on irc.freenode.net/#perl
,
use strict;
use warnings;
package Module {
use constant FOO => 42;
use Exporter 'import';
our @EXPORT = ('FOO');
}
my $subs = {
loc => sub {
package Foo;
Module->import();
my $result = FOO() * FOO();
},
fq => sub {
package Foo;
Module->import();
my $result = Module::FOO() * Module::FOO();
},
};
use Data::Dumper;
$Data::Dumper::Deparse = $Data::Dumper::Indent = 1;
print Dumper($subs);
That will result in,
$VAR1 = {
'loc' => sub {
package Foo;
use warnings;
use strict;
'Module'->import;
my $result = FOO() * FOO();
},
'fq' => sub {
package Foo;
use warnings;
use strict;
'Module'->import;
my $result = 1764;
}
};
You can see one of them has,
my $result = FOO() * FOO();
While the other has,
my $result = 1764;
You can get constant folding in both if the module is declared and the import is done in the compiler phase (in the case of a sub compilation), or before the execution in the case of a stringy-eval, like this
BEGIN {
package Module {
use constant FOO => 42;
use Exporter 'import';
our @EXPORT = ('FOO');
}
package Foo { Module->import() }
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…