3

鉴于这两个仅更改Module::FOO()和的评估FOO()

# Symbols imported, and used locally.
eval qq[ 
  package Foo$num;
  Module->import();
  my \$result = Module::FOO() * Module::FOO();
] or die $@;

# Symbols imported, not used locally referencing parent symbol.
eval qq[ 
  package Foo$num;
  Module->import();
  my \$result = FOO() * FOO();
] or die $@;

为什么顶部块占用的空间要少得多?脚本和输出复制如下,

脚本

package Module {
  use v5.30;
  use warnings;
  use constant FOO => 42; 
  use Exporter 'import';
  our @EXPORT = ('FOO');
}


package main {
  use v5.30;
  use autodie;
  use warnings;

  $|=1;
  say "Our PID is $$";

  for my $num ( 0..1e5 ) { 
    eval qq[ 
      package Foo$num;
      Module->import();
      my \$result = Module::FOO() * Module::FOO();
    ] or die $@;
    eval qq[
      package Foo$num;
      Module->import();
      my \$result = FOO() * FOO();
    ] or die $@;
  }

  say "DONE";

  _debug();

}

sub _debug {
  open my $fh, "<", "/proc/$$/status";
  while ( <$fh> ) { 
    next unless /Rss/;
    print;
  }
}

结果

包(命名空间)合格

RssAnon:      78896 kB
RssFile:       5220 kB
RssShmem:         0 kB

本地进口

RssAnon:     168180 kB
RssFile:       5496 kB
RssShmem:         0 kB
4

1 回答 1

4

这是 Perl 内部不断折叠的结果。这可以通过以下示例从haargon 开始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);

这将导致,

$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;
  }
};

你可以看到其中一个有,

my $result = FOO() * FOO();

而另一个有,

my $result = 1764;

如果模块被声明并且导入是在编译器阶段(在子编译的情况下)完成的,或者在 stringy-eval 的情况下执行之前,你可以得到常量折叠,像这样

BEGIN {

    package Module {
      use constant FOO => 42;
      use Exporter 'import';
      our @EXPORT = ('FOO');
    }
    package Foo { Module->import() }

}
于 2021-01-28T06:25:59.390 回答