Back to Top

Monday, November 20, 2006

You can do anything with Perl - part 2

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.


Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.