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

Good afternoon

I'm writing a test script that tests whether another perl script compiles correctly. If I was on the command line, I'd use perl -c foo.pl. Normally, if I was within a perl script, I'd either slurp the file into a string and eval it, or try eval { require 'foo.pl' } and check $@ for compilation errors

However, in this case, at the end of foo.pl, it enters a long-running loop with while (<>) (it's a Squid redirector script). This means that my test script hangs forever. How can I break out of this loop, and test whether it looked ok?

Thanks
ViceRaid

Replies are listed 'Best First'.
Re: Emulating -c within perl
by hardburn (Abbot) on Sep 12, 2003 at 15:06 UTC

    Call perl with backticks for each file (as in `perl -c $filename`;). eval isn't meant as a code checking system--it's for trapping errors and running dynamically-generated code.

    Also, be warned that any file with a BEGIN block will still have code executed under -c, so don't run this on any ol' code you find.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      You probably want to exchange "perl" with "$^X" inside the backticks to ensure you're using the same Perl as you're currently running with. Unless of course you want to check the behaviour in another version of Perl. ;-)

      Liz

      Thanks. After running `perl -c foo.pl`, how do I check what the result was? If I was running an eval, I'd check $@, but the return value from `` is just a string, so I can't check that directly.

      Update: aah, looks like I should be using $?. Also, thanks for the pointer, liz.