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? eval() just seems like a natural choice:

my $n = 0; my $result = undef; eval { $result = 10 / $n; }; if ($@) { print "can't do that!\n"; } else { print "here's your answer: $result\n"; }

But that won't work, if say, I want to test if another Perl script validates - and I also don't want the code to affect anything in my own program:

open my $perl, '<', 'perlscript.pl'; my $code = do { local $/; <$perl> }; close ($perl); eval {$perl}; # what am I eval'ing?

Since you know, who knows what that perl code actually does! That could lead to all sorts of bad things. do() also look enticing, but it still will run the code it... um, does, if there's no problems. Is there a,

eval_but_dont_execute($some_shaky_perl_code);

in existence? The more I think of it, the more I'm probably asking for a whole lot.

I'm trying to figure something out for a program who's config file format is just Perl code, instead of something intelligent like, INI or $Your_Favorite_Config_Format. Upgrades to the program could potentially hose an installation, if, for example, config variables in the config file don't exist anymore. Casual users who try to edit Perl code can also, easily host the installation.

-skazat

In reply to 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.