How to check if Perl compilation is complete?

I have code that requires the application to be fully downloaded and compiled before it runs.

Is there a way to check if my Perl program remains at compile time?

I have something that works, but there should be a better way:

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(); 

and subpkg.pm contains:

 package subpkg; use strict; use warnings; printf("initing subpkg\n"); main::check_compile(); 1; # successfully loaded 
+4
source share
3 answers

I assume that your code uses little eval - otherwise compilation will go "mixed" with execution, but you can consider using INIT and similar blocks here as a possible way to simplify this.

INIT blocks are run just before Perl's runtime runs at first-in, first-out (FIFO).

+6
source

See $^S in perlvar :

The current state of the interpreter.

  $ ^ S State
     --------- -------------------
     undef Parsing module / eval
     true (1) Executing an eval
     false (0) Otherwise

The first state can occur in the $ SIG { DIE } and $ SIG { WARN } handlers.

  BEGIN {print "Compile $ ^ S \ n"}
     print "Run-time $ ^ S \ n";
     eval {print "Eval $ ^ S \ n"};

  Compile
     Run time 0
     Eval 1
+2
source

Unconfirmed, but AFAIK should do this:

 sub main_run_phase_entered { local $@ ; ! eval q!use warnings FATAL => 'all'; CHECK{} 1! } 

Update: no, I did not succeed (from 5.10.1) in the END block {}. CHECK {} does not start, but also does not complain.

-1
source

All Articles