791Combining stderr and stdout 2>&1

If you ever find yourself in a situation of having to call a perl script from within PHP, and you want to get the return values from the perl script, you might do it like the following:

$command = "perl /my/perl/script.pl";
$results = exec($command);
// does not print error

If the perl script generates error, you won't be able to see them, as they are written to stderr.

One solution might be to append the stderr to stdout, therefore getting it into the $results variable.

$command = "perl /my/perl/script.pl 2>&1";
$results = exec($command);
// Prints: Died at /my/perl/script.pl line 25.

As this post explains, 1 means stdout, 2 means stderr. 2>1 might look ok at first sight, but the '1' will be interpreted as a filename. Therefore it has to be escaped with &1, resulting in 2>&1.