Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Capturing errors from backtick

by cormanaz (Deacon)
on Feb 26, 2020 at 15:39 UTC ( [id://11113442]=perlquestion: print w/replies, xml ) Need Help??

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

I am using backticks to run a Python script from the command line. It retrieves the publication date from html pages (if you're interested in that script see here): my $result = `htmldate -u $url`; When htmldate can't access the url, it writes an error to output # ERROR no valid result for url: and I'm wondering if there is a way to trap this error. The FAQ says "Backticks and open() read only the STDOUT of your command." I assume this means it assigns only the stdout output to $result in the above command, and it is writing the error message to whatever output is designated for stderr. Is there some way to detect this, short of redirecting stderr to a file, then opening, reading, and closing this every time I call htmldate?

Replies are listed 'Best First'.
Re: Capturing errors from backtick
by davido (Cardinal) on Feb 26, 2020 at 16:12 UTC

    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

Re: Capturing errors from backtick
by hippo (Bishop) on Feb 26, 2020 at 16:40 UTC

    The copy of the FAQs here in the monastery to which you linked (good for you for finding them, reading them and linking to the relevant one) are unfortunately well out of date.

    The maintained FAQ has some better advice such as using IPC::Open3, etc.

Re: Capturing errors from backtick
by haukex (Archbishop) on Feb 26, 2020 at 16:52 UTC

    For capturing both STDOUT and STDERR I'd suggest IPC::Run3 or Capture::Tiny. For all the details and example code see my node here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113442]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-20 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found