Perl shallow syntax check? ie. do not check syntax of imports

前端 未结 5 2251
一个人的身影
一个人的身影 2021-02-15 14:38

How can I perform a \"shallow\" syntax check on perl files. The standard perl -c is useful but it checks the syntax of imports. This is sometimes nice but not great

相关标签:
5条回答
  • 2021-02-15 14:48

    Have you looked into PPI? I think it does follow imports, however it could perhaps be more easily modified to guess what looks like a function name.

    0 讨论(0)
  • 2021-02-15 14:50

    There are two problems with this:

    1. How to not fail -c if the required modules are missing?

      There are two solutions:

      A. Add a fake/stub module in production

      B. In all your modules, use a special catch-all @INC subroutine entry (using subs in @INC is explained here). This obviously has a problem of having the module NOT fail in real production runtime if the libraries are missing - DoublePlusNotGood in my book.

    2. Even if you could somehow skip failing on missing modules, you would STILL fail on any use of the identifiers imported from the missing module or used explicitly from that module's namespace.

      The only realistic solution to this is to go back to #1a and use a fake stub module, but this time one that has a declared and (as needed) exported identifier for every public interface. E.g. do-nothing subs or dummy variables.

      However, even that will fail for some advanced modules that dynamically determine what to create in their own namespace and what to export in runtime (and the caller code could dynamically determine which subs to call - heck, sometimes which modules to import).

      But this approach would work just fine for normal "Java/C-like" OO or procedural code that only calls statically named predefined public subs, methods and accesses exported variables.

    0 讨论(0)
  • 2021-02-15 14:52

    I would suggest that it's better to include your code repository in your syntax check. perl -I/path/to/working/code/repo/local_perl/ -c or set PERL5LIB=/path/to/working/code/repo/local_perl/ prior to running perl -c. Either option should allow you to check against your working code, assuming you have it in a directory structure similar to your live code.

    0 讨论(0)
  • 2021-02-15 15:04

    I guess you could make stubs for the missing libraries in your home folder.

    0 讨论(0)
  • 2021-02-15 15:14

    It can't practically be done, because imports have the ability to influence the parsing of the code that follows. For example use strict makes it so that barewords aren't parsed as strings (and changes the rules for how variable names can be used), use constant causes constant subs to be defined, and use Try::Tiny changes the parse of expressions involving try, catch, or finally (by giving them & prototypes). More generally, any module that exports anything into the caller's namespace can influence parsing because the perl parser resolves ambiguity in different ways when a name refers to an existing subroutine than when it doesn't.

    0 讨论(0)
提交回复
热议问题