Perl Compilation Phase Detection

I work with a module that uses some prototypes to create blocks of code. For instance:

sub SomeSub (&) { ... }

Since prototypes only work when analyzing at compile time, I would like to throw a warning or even fatally if the module is not parsed at compile time. For instance:

require MyModule; # Prototypes in MyModule won't be parsed correctly

Is there a way to determine if something is running at compile time or / Perl runtime?

+4
source share
2 answers

Before 5.14 (either after or after) you can do:

package Foo;
BEGIN {
    use warnings 'FATAL' => 'all';
    eval 'INIT{} 1' or die "Module must be loaded during global compilation\n";
}

but this (and ${^GLOBAL_PHASE}) does not quite check what you want to know, namely, the code containing the use / require statement is compiled or run.

+4

Perl 5.14 , ${^GLOBAL_PHASE}, . .

use strict;
use warnings;

sub foo {
    if ( ${^GLOBAL_PHASE} eq 'START' ) {
        print "all good\n";
    } else {
        print "not in compile-time!\n";
    }
}

BEGIN {
    foo();
};

foo();

:

all good
not in compile-time!
+4

All Articles