📄 ch07_20.htm
字号:
<HTML><HEAD><TITLE>Recipe 7.19. Opening and Closing File Descriptors by Number (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:35Z"><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_19.htm"TITLE="7.18. Printing to Many Filehandles Simultaneously"><LINKREL="next"HREF="ch07_21.htm"TITLE="7.20. Copying Filehandles"></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_19.htm"TITLE="7.18. Printing to Many Filehandles Simultaneously"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.18. Printing to Many Filehandles Simultaneously"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_21.htm"TITLE="7.20. Copying Filehandles"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.20. Copying Filehandles"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-10025">7.19. Opening and Closing File Descriptors by Number</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1862">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009792-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009792-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009792-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009792-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009792-4"></A></A></H3><PCLASS="para">You know which file descriptors you'd like to do I/O on, but Perl requires filehandles, not descriptor numbers.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1868">Solution</A></H3><PCLASS="para">To open the file descriptor, use the <CODECLASS="literal">"<&="</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009795-0"></A> or <CODECLASS="literal">"<&"</CODE> open modes or the IO::Handle module's <CODECLASS="literal">fdopen</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009793-0"></A> class method:</P><PRECLASS="programlisting">open(FH, "<&=$FDNUM"); # open FH to the descriptor itselfopen(FH, "<&$FDNUM"); # open FH to a copy of the descriptoruse IO::Handle;$fh->fdopen($FDNUM, "r"); # open file descriptor 3 for reading</PRE><PCLASS="para">To close one by number, either use the <CODECLASS="literal">POSIX::close</CODE> function or else first open it as we did previously.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1888">Discussion</A></H3><PCLASS="para">Occasionally you have a file descriptor but no filehandle. Perl's I/O system uses filehandles instead of file descriptors, so you have to make a new filehandle for an already open file descriptor. The <CODECLASS="literal">"<&"</CODE>, <CODECLASS="literal">">&"</CODE>, and <CODECLASS="literal">"+<&"</CODE> modes to <CODECLASS="literal">open</CODE> will do this for reading, writing, and updating respectively. Adding an equal sign to these to make <CODECLASS="literal">"<&="</CODE>, <CODECLASS="literal">">&="</CODE>, and <CODECLASS="literal">"+<&="</CODE> is more parsimonious of file descriptors and nearly always what you want to do. That's because it used only a C-level <CODECLASS="literal">fdopen</CODE> function, not a <CODECLASS="literal">dup2</CODE> system call.</P><PCLASS="para">If you have version 5.004 or better installed, you can use the IO::Handle object method. This is the same as:</P><PRECLASS="programlisting">use IO::Handle;$fh = IO::Handle->new();$fh->fdopen(3, "r"); # open fd 3 for reading</PRE><PCLASS="para">Closing a file descriptor by number is even rarer. The <CODECLASS="literal">POSIX::close</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009794-0"></A> function does so directly. If your system doesn't have a working POSIX library but does have a working <CODECLASS="literal">syscall</CODE> (and your sysadmin has installed the <EMCLASS="emphasis">sys/syscall.ph</EM> file created with the <EMCLASS="emphasis">h2ph</EM> translator), you can call the not very portable <CODECLASS="literal">syscall</CODE> function. It has peculiar parameters and return values: You have to add 0 to numbers and append the empty string (<CODECLASS="literal">""</CODE>) to strings to coerce them to the right types for C, and the error return value is -1 rather than false as in other Perl calls. Needless to say, use <CODECLASS="literal">syscall</CODE> only as a last resort.</P><PCLASS="para">Here's how you'd open the file descriptors that the MH mail system feeds its child processes. It identifies them in the environment variable <CODECLASS="literal">MHCONTEXTFD</CODE>:</P><PRECLASS="programlisting">$fd = $ENV{MHCONTEXTFD};open(MHCONTEXT, "<&=$fd") or die "couldn't fdopen $fd: $!";# after processingclose(MHCONTEXT) or die "couldn't close context file: $!";</PRE><PCLASS="para">If you want to close a descriptor by number, just <CODECLASS="literal">open</CODE> it first.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1000011213">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">open</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 documentation for the standard POSIX and IO::Handle modules (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); your system's <ICLASS="filename">fdopen </I>(3) manpages</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_19.htm"TITLE="7.18. Printing to Many Filehandles Simultaneously"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.18. Printing to Many Filehandles Simultaneously"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_21.htm"TITLE="7.20. Copying Filehandles"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.20. Copying Filehandles"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.18. Printing to Many Filehandles Simultaneously</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.20. Copying Filehandles</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 + -