How can I read stderr when a command execution fails in Perl?

I am executing a diff command in perl.

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;

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

Appreciate the help in advance.

Thanks, Mathew Liju


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.

Good luck, :)

Need Your Help

Is it possible to add watermark to QR code generated by google charts api

php jquery cakephp google-visualization qr-code

Is it possible to add watermark to QR code generated by google charts api using jquery or cakephp.

C++ Boost random numeric generation problem

c++ boost random seed

I must generate a random number using boost libraries, I use this code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.