Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Printing to multiple file handles with one request

by gnu@perl (Pilgrim)
on Nov 19, 2002 at 15:24 UTC ( #214143=perlquestion: print w/replies, xml ) Need Help??
gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I have a situation where a program sends notifications in a number of methods (STDOUT, mailx, to a log file). Any one of a any combination of methods could be selected for the output under various circumstances.

What I was wondering is if I could use one print statement and specify multiple file handles to direct the print to?

I do realize that I could put the file handles into and array and loop over it, but I want to know if there is truly a way to do it with one execution of one command. Thanks.

  • Comment on Printing to multiple file handles with one request

Replies are listed 'Best First'.
Re: Printing to multiple file handles with one request
by tadman (Prior) on Nov 19, 2002 at 15:36 UTC
    You should create a sub-routine that handles these for you. For example:
    sub mprint { my @fh; push(@fh, shift) while (ref($_[0]) eq 'GLOB'); print $_ @_ foreach (@fh); } open($so, ">&STDOUT"); open($se, ">&STDERR"); mprint $se, $so, "This tests\n";
    It's not quite as clean as the builtin, but it's pretty close. You could even group your filehandles together into array packages, so that it's tidier:
    my @default = ($so); my @log_and_print = ($log, $so); mprint @default, "Standard message\n"; mprint @log_and_print, "This goes to log and screen\n";
    IO::Tee looks like a great way to do this as well, provided you can install your own modules.
      Yeah, this is pretty much what I had done, I was hoping there was a way 'built in' to standard perl. Installing modules is a little difficult due to the position of management.

      Sometimes I don't state what I have already done because it seems to taint the responses from time to time. I like to see fresh ideas and very often some really cool stuff come up.

      Thanks for your help!

Re: Printing to multiple file handles with one request
by RMGir (Prior) on Nov 19, 2002 at 15:35 UTC
    That looks like a job for IO::Tee.
Re: Printing to multiple file handles with one request
by John M. Dlugosz (Monsignor) on Nov 19, 2002 at 15:53 UTC
    I usually use a sub, like tadman explains.

    But, if you have code that writes to a single file already, you can retrofit by using a tie'd file handle. Just implement the WRITE, PRINT, and PRINTF hooks to send to your multiple sources.

Re: Printing to multiple file handles with one request
by jdporter (Canon) on Nov 19, 2002 at 19:17 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://214143]
Approved by RMGir
[TStanley]: I'm sitting in the airport in Tampa, Florida at the moment, getting ready to head home.
[talexb]: TStanley I'm flying to Vegas in two weeks time. After that flights leaving Vegas and Phoenix are late at night .. I've been hearing that it gets so hot that the air isn't dense enough for planes to take off. Weird. Guess you won't have that problem.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-06-23 14:13 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (548 votes). Check out past polls.