Search code examples
cperldynamiclibrariessyntax-checking

perl syntax check without loading c library


I would like to check syntax of my perl module (as well as for imports), but I don't want to check for dynamic loaded c libraries.

If I do:

perl -c path_to_module

I get:

Can't locate loadable object for module B::Hooks::OP::Check in @INC

because B::Hooks::OP::Check are loading some dynamic c libraries and I don't want to check that...


Solution

  • You can't.

    Modules can affect the scripts that use them in many ways, including how they are parsed.

    For example, if a module exports

    sub f() { }
    

    Then

    my $f = f+4;
    

    means

    my $f = f() + 4;
    

    But if a it were to export

    sub f { }
    

    the same code means

    my $f = f(+4);
    

    As such, modules must be loaded to parse the script that loads it. To load a module is simply to execute it, be it written in Perl or C.


    That said, some folks put together PPI to address the needs of people like you. It's not perfect —it can't be perfect for the reasons previously stated— but it will give useful results nonetheless.


    By the way, the proper way to syntax check a module is

    perl -e'use Module;'
    

    Using -c can give errors where non exists and vice-versa.