ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
7.18. Printing to Many Filehandles SimultaneouslyProblemYou need to output the same data to several different filehandles. SolutionIf you want to do it without forking, write a foreach $filehandle (@FILEHANDLES) { print $filehandle $stuff_to_print; } If you don't mind forking, open a filehandle that's a pipe to a tee program: open(MANY, "| tee file1 file2 file3 > /dev/null") or die $!; print MANY "data\n" or die $!; close(MANY) or die $!; DiscussionA filehandle sends output to one file or program only. To duplicate output to several places, you must call # `use strict' complains about this one: for $fh ('FH1', 'FH2', 'FH3') { print $fh "whatever\n" } # but not this one: for $fh (*FH1, *FH2, *FH3) { print $fh "whatever\n" } However, if your system supports the tee program, or if you've installed the Perl version from Recipe 8.19, you may open a pipe to tee and let it do the work of copying the file to several places. Remember that tee normally also copies its output to STDOUT, so you must redirect tee 's standard output to /dev/null if you don't want an extra copy: open (FH, "| tee file1 file2 file3 >/dev/null"); print FH "whatever\n"; You could even redirect your own STDOUT to the tee process, and then you're able to use a regular # make STDOUT go to three files, plus original STDOUT open (STDOUT, "| tee file1 file2 file3") or die "Teeing off: $!\n"; print "whatever\n" or die "Writing: $!\n"; close(STDOUT) or die "Closing: $!\n"; See AlsoThe |