I want to detect grammar errors in my perl code. I've found that perlcritic misses many of them, (eg a random else inserted before any if, so trying to compile with perl -cw looks like the only viable option.
However, I don't want to open myself up for executing code when checking for errors.
This perlmonks post shows that in BEGIN, INIT, UNITCHECK, and
CHECK blocks can / do get executed when compiling.
Can I grammar check perl code without running any of it?
What about removing or renaming the block which could cause execution?
Neither
perlcriticnorperltidyexecute any of the code they analyse/manipulate.To properly parse Perl code, part of it needs to be executed.
For example,
randomly outputs
5or7because the last statement is randomly compiled as one of the following:Ok, so that's pretty far-fetched. Or is it? How is that different than
Ok, so prototypes are discouraged. But what about
This means that correctly parsing code that uses the
sayoperator (not countingCORE::say) requires executing code. A lot code uses thesayoperator.But, if you account for a few common special cases, and if you accept a certain amount of imprecision (like whether
sayis a sub call or thesayoperator), one could parse Perl code fairly accurately without executing any of it. This is the idea behind PPI.perlcriticuses PPI. It doesn't execute any of the code it analyses.perltidyuses its own parser. It doesn't execute any of the code it analyses.perl -cwill executeBEGINblocks (includingusestatements) and the like.