in reply to How to do a "perl -c(w)" check instead of an "eval $string" on a code string

You're on the right track, but you need to know about the -e command line switch (try reading man perlrun). So you'll do something like this:
my $result = qx/ perl -cw -e '$code' 2>&1/;
Where you'll then need to scrape the $result string to find out if it passed or failed.

There's another angle to the problem though, you'll need to run the $code through some sort of shell quoting routine, to make sure you don't confuse the shell with characters in perl that also happen to be significant to the shell. I think the cpan module String::ShellQuote works okay for this.

(Note: updated, stealing a hint from shmem to append 2>&1 to the command, this makes sure you'll capture error messages to $result.)

  • Comment on Re: How to do a "perl -c(w)" check instead of an "eval $string" on a code string
  • Download Code

Replies are listed 'Best First'.
Re^2: How to do a "perl -c(w)" check instead of an "eval $string" on a code string
by Your Mother (Archbishop) on Jan 20, 2009 at 00:55 UTC

    Nice. I know -e, I figured it was out because of shell quoting but you've put me on the right track to at least play with that. Of course now that I write that I realize I could just open a pipe to perl...