Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Capturing errors from backtick

by davido (Cardinal)
on Feb 26, 2020 at 16:12 UTC ( [id://11113445]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113445]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found