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

skazat has asked for the wisdom of the Perl Monks concerning the following question:

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