You can do anything with Perl – part 2
I’m a PHP person myself and only the last several months did I start to play with Perl on a more serious level. One thing I’ve learned pretty fast: you can (almost) anything with Perl and the first thing you should do when you try to solve a problem is to look on the CPAN.
What I want to show today is how to capture the output that your own script produces. I wanted to emulate the ob_start / ob_get_contents combo from PHP. The main idea below is to replace the STDOUT handle with a pipe and after all the output was pushed to that pipe, retrieve it. One word of warning: the
close WRITE; is imperative. Without it the while loop reading the pipe would hang indefinitely.