Next Tricks of the Wizards 70

Tied Filehandles

        package TeeSTDOUT;
        sub import {
          my ($package, @outfiles) = @_;
          open REAL_STDOUT, ">&STDOUT" or die ...;
          my @handles;
          for my $outfile (@outfiles) {
            open my $fh, ">", $outfile or die ...;
            push @handles, $fh;
          }
          tie *STDOUT => 'TeeSTDOUT', \@handles;
        }
        sub TIEHANDLE {
          my ($package, $fhs) = @_;
          bless $fhs => $package;
        }
        sub PRINT {
          my ($fhs, $string) = @_;
          for my $outhandle (@$fhs, \*REAL_STDOUT) {
            print $outhandle $string;
          }
        }


Next Copyright © 2003 M. J. Dominus