One possibility for your case is the switch -c. This sort-of does what you want in that it

causes Perl to check the syntax of the program and then exit without executing it

assuming you don't use modules with side-effects or have side-effects in BEGIN, UNITCHECK, or CHECK blocks. By shelling it out, you guarantee not to muck up local variables. Perhaps something like:

my $script = 'perlscript.pl'; if (`perl -c $script 2>&1` =~ /syntax OK/) { print "$script compiles\n"; } else { print "Parsing $script failed\n"; }

Of course, this certainly fails on sorts of pathological cases. It might work as a stop gap for you, however, until you can bring your colleague over to your point of view.


In reply to Re: Testing if Perl Code is Valid - but don't execute! by kennethk
in thread Testing if Perl Code is Valid - but don't execute! by skazat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.