Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Answer: Is it possible to write to 2 files, eg. print LOGFILE,TEMP 'hello'?

by davido (Cardinal)
on Sep 26, 2003 at 07:50 UTC ( #294353=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > Is it possible to write to 2 files, eg. print LOGFILE,TEMP 'hello'? - Answer contributed by davido

From the Perl Cookbook.

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

And then an example adapted from the same section is:

my ($file1, $file2, $file3) = qw/this.out that.out other.out/; my $fh; open $fh, "| tee $file1 $file2 $file3 > /dev/null" or die $!; print $fh "data\n" or die $!; close $fh or die $!;

Of course this will only be of value to those whos operating systems support tee (Linux / Unix, for example). The reason for the redirection to /dev/null is because tee typically copies its output on STDOUT. If you don't want that extra copy you redirect it to /dev/null (the garbage can).

  • Comment on Answer: Is it possible to write to 2 files, eg. print LOGFILE,TEMP 'hello'?
  • Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2020-11-29 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?