⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07_16.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 7.15. Determining the Number of Bytes to Read (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:37:19Z"><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_15.htm"TITLE="7.14. Doing Non-Blocking I/O"><LINKREL="next"HREF="ch07_17.htm"TITLE="7.16. Storing Filehandles in Variables"></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_15.htm"TITLE="7.14. Doing Non-Blocking I/O"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.14. Doing Non-Blocking I/O"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_17.htm"TITLE="7.16. Storing Filehandles in Variables"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.16. Storing Filehandles in Variables"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-16091">7.15. Determining the Number of Bytes to Read</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1500">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009725-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009725-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009725-2"></A></A></H3><PCLASS="para">You want to know how many bytes to read from a filehandle with <CODECLASS="literal">read</CODE> or <CODECLASS="literal">sysread</CODE>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1506">Solution</A></H3><PCLASS="para">Use the FIONREAD ioctl call:</P><PRECLASS="programlisting">$size = pack(&quot;L&quot;, 0);ioctl(FH, $FIONREAD, $size)     or die &quot;Couldn't call ioctl: $!\n&quot;;$size = unpack(&quot;L&quot;, $size);# $size bytes can be read</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1522">Discussion</A></H3><PCLASS="para">The Perl <CODECLASS="literal">ioctl</CODE> function is a direct interface to the operating system's <EMCLASS="emphasis">ioctl (2)</EM> system call. If your system doesn't have the FIONREAD request or the <EMCLASS="emphasis">ioctl (2)</EM> call, you can't use this recipe. FIONREAD and the other <EMCLASS="emphasis">ioctl</EM> (2) requests are numeric values normally found lurking C include files.</P><PCLASS="para">Perl's <EMCLASS="emphasis">h2ph</EM> tool tries to convert C include files to Perl code, which can be <CODECLASS="literal">require</CODE>d. FIONREAD ends up defined as a function in the <EMCLASS="emphasis">sys/ioctl.ph</EM> file:</P><PRECLASS="programlisting">require 'sys/ioctl.ph';$size = pack(&quot;L&quot;, 0);ioctl(FH, FIONREAD(), $size)    or die &quot;Couldn't call ioctl: $!\n&quot;;$size = unpack(&quot;L&quot;, $size);</PRE><PCLASS="para">If <EMCLASS="emphasis">h2ph</EM> wasn't installed or doesn't work for you, you can manually <EMCLASS="emphasis">grep</EM> the include files:</P><PRECLASS="programlisting">% grep FIONREAD /usr/include/*/*<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>/usr/include/asm/ioctls.h:#define FIONREAD      0x541B</I></CODE></B></CODE></PRE><PCLASS="para">Or write a small C program using the editor of champions:</P><PRECLASS="programlisting">% cat &gt; fionread.c#include &lt;sys/ioctl.h&gt;<CODECLASS="literal">main() {</CODE>    printf(&quot;%#08x\n&quot;, FIONREAD);}^D% cc -o fionread fionread% ./fionread<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>0x4004667f</I></CODE></B></CODE></PRE><PCLASS="para">Then hard-code it, leaving porting as an exercise to your successor.</P><PRECLASS="programlisting">$FIONREAD = 0x4004667f;         # XXX: opsys dependent$size = pack(&quot;L&quot;, 0);ioctl(FH, $FIONREAD, $size)     or die &quot;Couldn't call ioctl: $!\n&quot;;$size = unpack(&quot;L&quot;, $size);</PRE><PCLASS="para">FIONREAD requires a filehandle connected to a stream, which means sockets, pipes, and tty devices work, but files don't.</P><PCLASS="para">If this is too much system programming for you, try to think outside the problem. Read from the filehandle in non-blocking mode (see <ACLASS="xref"HREF="ch07_15.htm"TITLE="Doing Non-Blocking I/O">Recipe 7.14</A>). If you manage to read something, then that's how much was waiting to be read. If you couldn't read anything, there was nothing to be read. <ACLASS="indexterm"NAME="ch07-idx-1000009727-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009727-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009727-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1582">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch07_15.htm"TITLE="Doing Non-Blocking I/O">Recipe 7.14</A>; your system's <ICLASS="filename">ioctl</I> (2) manpage; the <CODECLASS="literal">ioctl</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></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_15.htm"TITLE="7.14. Doing Non-Blocking I/O"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.14. Doing Non-Blocking I/O"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_17.htm"TITLE="7.16. Storing Filehandles in Variables"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.16. Storing Filehandles in Variables"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.14. Doing Non-Blocking I/O</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.16. Storing Filehandles in Variables</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 &copy; 2002</a> O'Reilly &amp; 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 + -