📄 ch07_19.htm
字号:
<HTML><HEAD><TITLE>Recipe 7.18. Printing to Many Filehandles Simultaneously (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:37:34Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch07_01.htm"TITLE="7. File Access"><LINKREL="prev"HREF="ch07_18.htm"TITLE="7.17. Caching Open Output Filehandles"><LINKREL="next"HREF="ch07_20.htm"TITLE="7.19. Opening and Closing File Descriptors by Number"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_18.htm"TITLE="7.17. Caching Open Output Filehandles"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.17. Caching Open Output Filehandles"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch07_01.htm"TITLE="7. File Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_20.htm"TITLE="7.19. Opening and Closing File Descriptors by Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.19. Opening and Closing File Descriptors by Number"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-chap07_printing_0">7.18. Printing to Many Filehandles Simultaneously</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1796">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009786-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009786-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009786-2"></A></A></H3><PCLASS="para">You need to output the same data to several different filehandles.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1802">Solution</A></H3><PCLASS="para">If you want to do it without forking, write a <CODECLASS="literal">foreach</CODE> loop that iterates across the filehandles:</P><PRECLASS="programlisting">foreach $filehandle (@FILEHANDLES) { print $filehandle $stuff_to_print;}</PRE><PCLASS="para">If you don't mind forking, open a filehandle that's a pipe to a <EMCLASS="emphasis">tee</EM> program:</P><PRECLASS="programlisting">open(MANY, "| tee file1 file2 file3 > /dev/null") or die $!;print MANY "data\n" or die $!;close(MANY) or die $!;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1822">Discussion</A></H3><PCLASS="para">A filehandle sends output to one file or program only. To duplicate output to several places, you must call <CODECLASS="literal">print</CODE> multiple times or make a filehandle connected to a program like <CODECLASS="literal">tee</CODE>, which distributes its input elsewhere. If you use the first option, it's probably easiest to put the filehandles in a list or array and loop through them:</P><PRECLASS="programlisting"># `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" }</PRE><PCLASS="para">However, if your system supports the <EMCLASS="emphasis">tee</EM> program, or if you've installed the Perl version from <ACLASS="xref"HREF="ch08_20.htm"TITLE="Program: tctee">Recipe 8.19</A>, you may open a pipe to <EMCLASS="emphasis">tee</EM> and let it do the work of copying the file to several places. Remember that <EMCLASS="emphasis">tee</EM> normally also copies its output to STDOUT, so you must redirect <EMCLASS="emphasis">tee </EM>'s standard output to <EMCLASS="emphasis">/dev/null</EM> if you don't want an extra copy:</P><PRECLASS="programlisting">open (FH, "| tee file1 file2 file3 >/dev/null");print FH "whatever\n";</PRE><PCLASS="para">You could even redirect your own STDOUT to the <EMCLASS="emphasis">tee</EM> process, and then you're able to use a regular <CODECLASS="literal">print</CODE> directly:</P><PRECLASS="programlisting"># make STDOUT go to three files, plus original STDOUTopen (STDOUT, "| tee file1 file2 file3") or die "Teeing off: $!\n";print "whatever\n" or die "Writing: $!\n";close(STDOUT) or die "Closing: $!\n";<ACLASS="indexterm"NAME="ch07-idx-1000009788-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009788-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009788-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1852">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">print</CODE> function in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.6">"Typeglobs and Filehandles"</A> sections of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; we use this technique in <ACLASS="xref"HREF="ch08_20.htm"TITLE="Program: tctee">Recipe 8.19</A> and <ACLASS="xref"HREF="ch13_16.htm"TITLE="Creating Magic Variables with tie">Recipe 13.15</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_18.htm"TITLE="7.17. Caching Open Output Filehandles"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.17. Caching Open Output Filehandles"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_20.htm"TITLE="7.19. Opening and Closing File Descriptors by Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.19. Opening and Closing File Descriptors by Number"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.17. Caching Open Output Filehandles</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">7.19. Opening and Closing File Descriptors by Number</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -