dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

I usually use "perl -c" on my (or other people's) scripts prior to commit/deployment/bundling a release tarball. Since "perl -c" executes BEGIN blocks, is there an alternatifve that is safer (i.e. no side effects, syntax check only)? I occasionally use Komodo Editor and am aware of its real-time syntax check feature. I also have heard about PPI. But beyond that I'm clueless. :-)

Replies are listed 'Best First'.
Re: perl -c and BEGIN block
by ikegami (Patriarch) on Mar 19, 2008 at 18:17 UTC

    It's impossible to check syntax without executing BEGIN blocks. The code in in BEGIN blocks including used modules can affect the parsing of subsequent code.

    e.g. Does
    $x = func + 2;
    mean
    $x = func() + 2;
    or
    $x = func(2);
    or is it a syntax error such as
    Not enough arguments

    It could be any of the three. There's no way to know which without loading the module and calling it's import function from a BEGIN block.

    Update: Added substantiation in the form of an example.

Re: perl -c and BEGIN block
by samtregar (Abbot) on Mar 19, 2008 at 17:07 UTC
    PPI is indeed your only alternative that I'm aware of. But if you're deploying code aren't you going to be vulnerable to a misbehaving BEGIN eventually anyway?

    -sam

Re: perl -c and BEGIN blockc (safe)
by ikegami (Patriarch) on Mar 19, 2008 at 18:36 UTC
    Safe might be of interest.