I have a Perl script that takes user input and creates another script that will be run at a later date. I’m currently going through and writing tests for these scripts and one of the tests that I would like to perform is checking if the generated script compiles successfully (e.g. perl -c <script>.) Is there a way that I can have Perl perform a compile on the generated script without having to spawn another Perl process? I’ve tried searching for answers, but searches just turn up information about compiling Perl scripts into executable programs.
I have a Perl script that takes user input and creates another script that
Share
To execute dynamically generated code, use
evalfunction:However if you want to just compile or check syntax, then you will not be able to do it within same Perl session.
Function
syntax_okfrom library Test::Strict run a syntax check by runningperl -cwith an external perl interpreter, so I assume there is no internal way.Only work-around that may work for you would be:
In this case, you will be able to check for compilation error(s) using
$@, however because the first line of the code isreturn;, it will not execute.Note: Thanks to user mob for helpfull chat and code correction.