http://www.perlmonks.org?node_id=854269


in reply to Re^2: Testing if Perl Code is Valid - but don't execute!
in thread Testing if Perl Code is Valid - but don't execute!

Even with executing the BEGIN block, you still only have a 50% success rate.
No. After executing the BEGIN block, you can determine in 100% of the cases whether the rest of the code is valid or not. (50% of the time it will be valid, and 50% of the time it will not. But at that stage, you can tell whether it's valid or not. And that's the question to be answered.)
  • Comment on Re^3: Testing if Perl Code is Valid - but don't execute!

Replies are listed 'Best First'.
Re^4: Testing if Perl Code is Valid - but don't execute!
by merlyn (Sage) on Aug 13, 2010 at 02:27 UTC
    No, I would argue that you have to finish parsing the file. Which puts it at a 50% success rate. Unless you have knowledge ahead of time of all possible environments that could succeed or fail in the remaining code.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      Well, duh, you got to finish parsing the file to know whether it's valid or not.

      For the example given though, you don't to execute more than the BEGIN block. That is, afterall, exactly what perl -c does. It executes the BEGIN, parses the file, and then, 100% of the time, tells you whether the file contains a valid Perl program or not.