Posts Tagged ‘perl’

PHP & Perl: How do I redirect all output from STDOUT or STDERR into a file?

The power of PHP often goes without question, however once in a while we remember a little something from Perl that we just wish was as easy in PHP. The truth is, so much is easier with PHP than with Perl that PHP is just awesome, but hey – it’s a programming language – it’s going to have shortcomings. The most common reason we are asked this is for crons – scheduled programs, batches, and such. Often a programmer will want to redirect the output to a certain file, or to /dev/null. In Perl, you are able to do this easy-as-cheese:

open (STDOUT, ">/path/to/file") or die "Can't redirect STDOUT: $!";

open (STDERR, ">>/path/to/file") or die "Can't redirect STDERR: $!";

Please take note: PERL redirects a filehandle in one line!

However, PHP is a bit of a different story: (more…)