Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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";
    Update:
    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.
    --
    Mike
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2015-07-31 09:24 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 (276 votes), past polls