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

The jist is I want to execute a system command without worrying about shell metacharacters or quoting, and I'd also like to capture both STDOUT and STDERR.

system PROGRAM LIST is an obvious place to start. This seems to avoid the shell like I wanted, but I can't capture the output. To do that, the docs suggest I use backticks. Backticks let me get the output, but now I have to worry about shell metacharacters again. Using open also seems to rely on the shell.

So... is there a shell escape function I can use? Or is there a module to safely handle spawning and communicating with processes? Or some other standard practice? What's the way to do this?

Replies are listed 'Best First'.
Re: Safe Execute System Command and Capture Output
by Anonymous Monk on Aug 28, 2014 at 23:08 UTC
    IPC::Run3 does everything you want, I've used it often. There's also IPC::Run, it's more advanced and handles things like timeouts too, but the test reports and the bug count seem to show it doesn't work perfectly everywhere. Lastly, there's Capture::Tiny, which you can wrap around your system call. (There's lots more similar modules, but these three have always seemed "best" to me - most features, best platform support.)