<p>I am executing a <code>diff</code> command in <code>perl</code>.</p>

<pre><code>my @lines = `/usr/local/bin/diff -udr \"$expected_file\" \"$gen_file\"`;

if ($? != 0)
{
    print ERRFILE "Diff between $expected_file and $gen_file failed\n";
    return $diff_err;
}
</code></pre>

<p>Here the <code>diff</code> might have failed because of some reason. For example: the stderr showed that <em>/usr/local/bin/diff: test.txt: No such file or directory</em>. I want to read this message in the program. How I can find the stderr message of the <code>diff</code> command (or <code>grep</code> or any command I execute)?</p>

<p>Appreciate the help in advance.</p>

<p>Thanks,
Mathew Liju</p>

This is answered in
perlfaq8: How can I capture STDERR from an external command?
If I want to read STDOUT and STDERR of a process, I use IPC::Open3, which comes with Perl. That way, I don't have to merge those streams and later figure out what part of the output came from what.
I would try to avoid temporary files whenever possible (so no
```
2>file.txt
```
). That's just too much work and code when I can read STDERR directly.