Perl Advent Calendar 2008-12-08

Running in a filtered wonderland

by Jerrad Pierce

The jolly old elf enjoys milk and cookies, no spam and phish, so he recently deployed a SpamAssassin solution for his network. However, he had a bunch of legacy systems that needed to be plugged into the workflow as well, so he attempted to parse all incoming mail through a filter that would pass messages through spamc after the other message mangling had taken place. All that business of daemons with forks and zombie children sent shivers down his spine, so he decided to work with pipes. The Friendly Manual suggested IPC::Open2 as the solution to his naive open(SPAM, '|/bin/spamc|'), and soon Santa discovered that classic toy though they may be, blocks are no fun when it comes to interprocess communication.

Had Santa searched CPAN he might have discovered IPC::Filter, a module intended to simplify the exchange of data between a script and sieving subprocess. To do so, it wraps IPC::Open3 up tightly along with other goodies to yield such an amazingly simple interface that there's not much left to be said:

use IPC::Filter 'filter';

$b = filter($a, '/usr/bin/spamc');