in reply to syntax check from within perl

Just do exactly what perl -c does:

eval "return; $code";

Replies are listed 'Best First'.
Re (tilly) 2: syntax check from within perl
by tilly (Archbishop) on Mar 03, 2002 at 04:34 UTC
    But see discussion elsewhere in the thread.

    The reader did not realize that perl -c does not provide a safe way to test syntactical correctness. There is no way to do that in Perl.

    For instance suppose that it does a use. There is no way to know if later code is syntactically correct without running the used module. (For instance what functions are exported? This affects what will be trapped as a bareword under strict, and what will not.) But when you do that, the module could do absolutely anything it wanted to.

      But safety wasn't the question - merely how to emulate perl -c...

      If you want it safe from malicious intent, we all know you're basically screwed with Perl. You can't not have it execute use bits, because many imports affect the compilation, like use constant. So either you test the compilation or you don't. I believe the author asked to do so.