如何检查Perl编译是否已经完成?

mmc*_*coo 4 perl

我有一些代码要求应用程序在执行之前完全加载和编译.

有没有办法检查我的Perl程序是否仍处于编译阶段?

我有一些有用的东西,但必须有一个更好的方法:

sub check_compile {
  printf("checking\n");
  foreach my $depth (0..10) {
    my ($package, $filename, $line, $subroutine) = caller($depth);
    last unless (defined($package));

    printf("  %s %s %s %s %s\n", $depth, $package, $filename, $line, $subroutine);

    if ($subroutine =~ /::BEGIN$/) {
      printf("in compile\n");
      return 1;
    }

  }

  printf("not in compile\n");
  return 0;
}


BEGIN {
  check_compile();
}

use subpkg;

check_compile();
Run Code Online (Sandbox Code Playgroud)

和subpkg.pm包含:

package subpkg;

use strict;
use warnings;

printf("initing subpkg\n");
main::check_compile();

1; # successfully loaded
Run Code Online (Sandbox Code Playgroud)

mar*_*ton 6

我假设您的代码没有太多用处eval- 否则编译将与执行一起"混入" - 但您可以在此处查看使用INIT和类似的块作为简化此操作的可能方法.

INIT块在Perl运行时开始执行之前以"先进先出"(FIFO)顺序运行.