http://qs321.pair.com?node_id=11113445


in reply to Capturing errors from backtick

If you look at the documentation in perlop for backticks it provides several strategies; shell redirection of STDERR to STDOUT, swapping STDERR and STDOUT, and even just writing STDERR to a file that you can look at later. It's entirely possible none of these are convenient for your use case. I'd recommend instead going to a module like Capture::Tiny where your code would look something like this:

use Capture::Tiny qw(capture); my ($stdout, $stderr, $exit_code) = capture { system('htmldate', '-u', $url) };

I used the list form of system as well, to avoid exposing $url to the shell; this is a bit of a safety best practice.

You can see here that using the capture function you can run your external call as a system call, and then capture STDOUT, STDERR, and the exit code into distinct variables that you can then inspect after the call.

The Capture::Tiny module consists of less than 430 lines of well-tested code, and has no non-core Perl dependencies, so it should be quite easy to install.


Dave