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

前端之家收集整理的这篇文章主要介绍了如何检查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,$subroutine);

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

  }

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


BEGIN {
  check_compile();
}

use subpkg;

check_compile();

和subpkg.pm包含:

package subpkg;

use strict;
use warnings;

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

1; # successfully loaded

解决方法

我假设您的代码没有充分利用eval – 否则编译将与执行一起“混入” – 但您可能会考虑使用INIT和类似的块 here作为简化此操作的可能方法.

INIT blocks are run just before the Perl runtime begins execution,in “first in,first out” (FIFO) order.

猜你在找的Perl相关文章