Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

IO::MultiHandle - Operate on multiple file handles as one

by jdporter (Canon)
on Nov 19, 2002 at 19:11 UTC ( #214219=snippet: print w/ replies, xml ) Need Help??

Description: This module encapsulates any number of filehandles in a kind of "meta-filehandle". Any operations you perform on the meta-filehandle are delegated to all of the contained handles. This may be similar to the IO::Tee module.

This solution was developed in response to Printing to multiple file handles with one request.

Updated in response to helpful comments from jmcnamara and Aristotle.


=head1 NAME

IO::MultiHandle - Encapsulate multiple handles in one.

=head1 SYNOPSIS

    use IO::MultiHandle;

    my $logfh = IO::File->new( "> $outfile" ) or die "write $outfile: 
+$!";

    # create a multi-handle which will write to both
    # STDERR and the filehandle created above:
    my $mfh = new IO::MultiHandle *STDERR, $logfh;

    print $mfh "A line of output.\n";

=cut


package Tie::MultiHandle; # do not use directly.
  use IO::Handle;

  sub TIEHANDLE
  {
    my $pkg = shift;
    bless [ @_ ], $pkg
  }

  sub AUTOLOAD
  {
    my( $self, @args ) = @_;
    our( $AUTOLOAD );
    $AUTOLOAD =~ s/.*::(.*)/\L$1/;
    for my $fh ( @$self ) { $fh->$AUTOLOAD( @args ); }
  }

  sub DESTROY
  {
  }


package IO::MultiHandle;
  use Symbol;

  # takes a list of handles
  sub new
  {
    my $pkg = shift;
    my $self = gensym;
    tie *$self, Tie::MultiHandle::, @_;
    bless $self, $pkg
  }

Comment on IO::MultiHandle - Operate on multiple file handles as one
Select or Download Code
Re: Print to multiple file handles at once.
by jmcnamara (Monsignor) on Nov 20, 2002 at 15:58 UTC

    That is a nice idea and a nice implementation. However, it has some problems.

    The my $fh won't work with the postfix for. You would have to do something like this instead:      $_->$AUTOLOAD(@args) for @$self;


    Also, the following won't work because MultiHandle->new() doesn't return a GLOB:     print $mfh "A line of output.\n";


    The following does work but it isn't as pretty:     $mfh->print("A line of output.\n");    

    --
    John.

Re: Print to multiple file handles at once.
by Aristotle (Chancellor) on Nov 20, 2002 at 16:18 UTC
    Did you test this? The following won't work: $fh->$AUTOLOAD( @args ) for my $fh @$self;
    Perl hasn't seen the my when it sees the first mention of $fh so that one refers to the global variable of that name. Either declare the variable on a line by itself or use $_. $_->$AUTOLOAD(@args) for @$self;

    Makeshifts last the longest.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://214219]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2015-07-04 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls