Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Testing if Perl Code is Valid - but don't execute!

by JavaFan (Canon)
on Aug 10, 2010 at 21:16 UTC ( [id://854139]=note: print w/replies, xml ) Need Help??


in reply to Testing if Perl Code is Valid - but don't execute!

Is there a way to test if Perl code is valid, without having the Perl code you want to validate, affect the rest of the script?
No. Consider:
BEGIN { my $p = rand() < .5 ? '$' : ''; eval "sub foo($p) {1;}"; } foo / 1;
Valid or not? Without executing the begin block, impossible to answer. (It's valid if the prototype is empty, it's a syntax error otherwise.)

Replies are listed 'Best First'.
Re^2: Testing if Perl Code is Valid - but don't execute!
by merlyn (Sage) on Aug 11, 2010 at 00:39 UTC
    Even with executing the BEGIN block, you still only have a 50% success rate. :)

    For more examples like this, check my seminal classic On Parsing Perl.

    -- 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.

      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.)
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://854139]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found