[San-Diego-pm] Module idea for input/output multiplexing

Daniel Risse dan at tierra.net
Wed Dec 13 11:23:25 PST 2006

I thought I recalled this form the Perl Cookbook (2nd edition).  Sure 
enough, there is an example showing a few ways to accomplish this.  It's 
on page 260 for those of you with the book.

If you want to do it without forking, use a foreach loop

foreach $fh (@FHS) {
  print {$fh} $stuff;

If you don't mind forking, open a filehandle that's a pipe to tee.

open my $multi, '| tee file1 file2 file3 > /dev/null' or die $!;
print {$multi} $stuff;
close $multi;

Or you can use IO::Tee from CPAN

use IO::Tee;
my $tee = IO::Tee->new(@FHS);
print {$tee} $stuff;
close $tee;

I think Chris's Proc::Multiplex is a cleaner interface though.

More information about the San-Diego-pm mailing list