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

📄 ch07_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Formats (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Formats"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch06_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch07_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 7.  Formats</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch07_01.htm">Format Variables</a><br><a href="ch07_02.htm">Footers</a><br></p></div><a name="INDEX-1884"></a><p><a name="INDEX-1885"></a><a name="INDEX-1886"></a>Perl has a mechanism to help you generate simple reports and charts.To facilitate this, Perl helps you code up your output page close tohow it will look when it's printed.  It can keep track of things likehow many lines are on a page, the current page number, when to printpage headers, and so on. Keywords are borrowed from FORTRAN:<tt class="literal">format</tt> to declare and <tt class="literal">write</tt> toexecute; see the relevant entries in <a href="ch29_01.htm">Chapter 29, "Functions"</a>.  Fortunately, the layout is much morelegible, more like the <tt class="literal">PRINT USING</tt> statement ofBASIC. Think of it as a poor man's<em class="emphasis">nroff</em>(1).  (If you know<em class="emphasis">nroff</em>, that may not sound like a recommendation.)</p><p><a name="INDEX-1887"></a><a name="INDEX-1888"></a><a name="INDEX-1889"></a><a name="INDEX-1890"></a>Formats, like packages and subroutines, are declared rather thanexecuted, so they may occur at any point in your program.  (Usuallyit's best to rukeep them all together.) They have their own namespaceapart from all the other types in Perl.  This means that if you have afunction named "<tt class="literal">Foo</tt>", it is not the same thing as aformat named "<tt class="literal">Foo</tt>".  However, the default name forthe format associated with a given filehandle is the same as the nameof that filehandle.  Thus, the default format for<tt class="literal">STDOUT</tt> is named "<tt class="literal">STDOUT</tt>", andthe default format for filehandle <tt class="literal">TEMP</tt> is named"<tt class="literal">TEMP</tt>".  They just look the same.  They aren't.</p><p><a name="INDEX-1891"></a>Output record formats are declared as follows:<blockquote><pre class="programlisting">format <em class="replaceable">NAME</em> =<em class="replaceable">FORMLIST</em>.</pre></blockquote>If <em class="replaceable">NAME</em> is omitted, format <tt class="literal">STDOUT</tt> is defined. <em class="replaceable">FORMLIST</em> consistsof a sequence of lines, each of which may be of one of three types:</p><ul><li><p>A comment, indicated by putting a <tt class="literal">#</tt> in the first column.</p></li><li><p>A "picture" line giving the format for one output line.</p></li><li><p>An argument line supplying values to plug into the previous pictureline.</p></li></ul><a name="INDEX-1892"></a><a name="INDEX-1893"></a><a name="INDEX-1894"></a><a name="INDEX-1895"></a><p>Picture lines are printed exactly as they look, except for certainfields that substitute values into the line.<a href="#FOOTNOTE-1">[1]</a> Each substitution field in a picture linestarts with either <tt class="literal">@</tt> (at) or <tt class="literal">^</tt>(caret).  These lines do not undergo any kind of variableinterpolation.  The <tt class="literal">@</tt> field (not to be confusedwith the array marker <tt class="literal">@</tt>) is the normal kind offield; the other kind, the ^ field, is used to do rudimentarymultiline text-block filling.  The length of the field is supplied bypadding out the field with multiple <tt class="literal">&lt;</tt>,<tt class="literal">&gt;</tt>, or <tt class="literal">|</tt> characters tospecify, respectively, left justification, right justification, orcentering.  If the variable exceeds the width specified, it istruncated.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] Eventhose fields maintain the integrity of the columns you put them in,however.  There is nothing in a picture line that can cause fields togrow or shrink or shift back and forth.  The columns you see aresacred in a WYSIWYG sense--assuming you're using a fixed-widthfont.  Even control characters are assumed to have a width ofone.</p></blockquote><p><a name="INDEX-1896"></a><a name="INDEX-1897"></a><a name="INDEX-1898"></a>As an alternate form of right justification, you may also use<tt class="literal">#</tt> characters (after an initial <tt class="literal">@</tt>or <tt class="literal">^</tt>) to specify a numeric field.  You can insert a<tt class="literal">.</tt> in place of one of the <tt class="literal">#</tt>characters to line up the decimal points.  If any value supplied forthese fields contains a newline, only the text up to the newline isprinted. Finally, the special field <tt class="literal">@*</tt> can be usedfor printing multiline, nontruncated values; it should generallyappear on a picture line by itself.</p><p>The values are specified on the following line in the same order asthe picture fields.  The expressions providing the values should beseparated by commas.  The expressions are all evaluated in a listcontext before the line is processed, so a single list expressioncould produce multiple list elements.  The expressions may be spreadout to more than one line if enclosed in braces.  (If so, the openingbrace must be the first token on the first line).  This lets you lineup the values under their respective format fields for easier reading.</p><p><a name="INDEX-1899"></a>If an expression evaluates to a number with a decimal part, and if thecorresponding picture specifies that the decimal part should appear inthe output (that is, any picture except multiple <tt class="literal">#</tt>characters without an embedded <tt class="literal">.</tt>), the characterused for the decimal point is always determined by the current<tt class="literal">LC_NUMERIC</tt> locale.  This means that if, forexample, the run-time environment happens to specify a German locale,a comma will be used instead of a period.  See the<em class="emphasis">perllocale</em> manpage for more information.</p><p><a name="INDEX-1900"></a>Inside an expression, the whitespace characters <tt class="literal">\n</tt>,<tt class="literal">\t</tt>, and <tt class="literal">\f</tt> are all consideredequivalent to a single space.  Thus, you could think of this filter asbeing applied to each value in the format:<blockquote><pre class="programlisting">$value =~ tr/\n\t\f/ /;</pre></blockquote>The remaining whitespace character, <tt class="literal">\r</tt>, forces theprinting of a new line if the picture line allows it.</p><p><a name="INDEX-1901"></a>Picture fields that begin with <tt class="literal">^</tt> rather than<tt class="literal">@</tt> are treated specially.  With a<tt class="literal">#</tt> field, the field is blanked out if the value isundefined.  For other field types, the caret enables a kind of fillmode.  Instead of an arbitrary expression, the value supplied must bea scalar variable name that contains a text string.  Perl puts as muchtext as it can into the field, and then chops off the front of thestring so that the next time the variable is referenced, more of thetext can be printed. (Yes, this means that the variable itself isaltered during execution of the <tt class="literal">write</tt> call and isnot preserved. Use a scratch variable if you want to preserve theoriginal value.)  Normally you would use a sequence of fields lined upvertically to print out a block of text.  You might wish to end thefinal field with the text "<tt class="literal">...</tt>", which willappear in the output if the text was too long to appear in itsentirety.  You can change which characters are legal to "break" on (orafter) by changing the variable <tt class="literal">$:</tt> (that's<tt class="literal">$FORMAT_LINE_BREAK_CHARACTERS</tt> if you're using the<tt class="literal">English</tt> module) to a list of the desiredcharacters.</p><p><a name="INDEX-1902"></a><a name="INDEX-1903"></a><a name="INDEX-1904"></a>Using <tt class="literal">^</tt> fields can produce variable-length records.  If the text to beformatted is short, just repeat the format line with the ^ field in it afew times.  If you just do this for short data you'd end up getting afew blank lines.  To suppress lines that would end up blank, put a <tt class="literal">~</tt>(tilde) character anywhere in the line.  (The tilde itself will betranslated to a space upon output.)  If you put a second tildenext to the first, the line will be repeated until all the text inthe fields on that line are exhausted.  (This works because the <tt class="literal">^</tt>fields chew up the strings they print.  But if you use a field of the<tt class="literal">@</tt> variety in conjunction with two tildes, the expression you supplyhad better not give the same value every time forever!  Use a <tt class="literal">shift</tt>,or some other operator with a side effect that exhausts the set ofvalues.)</p><p><a name="INDEX-1905"></a><a name="INDEX-1906"></a><a name="INDEX-1907"></a>Top-of-form processing is by default handled by a format with the samename as the current filehandle with <tt class="literal">_TOP</tt> concatenated to it.It's triggered at the top of each page.  See <tt class="literal">write</tt> in <a href="ch29_01.htm">Chapter 29, "Functions"</a>.</p><p>Here are some examples:<blockquote><pre class="programlisting"># a report on the /etc/passwd fileformat STDOUT_TOP =                         Passwd FileName                Login    Office   Uid   Gid Home------------------------------------------------------------------.format STDOUT =@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @||||||| @&lt;&lt;&lt;&lt;&lt;&lt;@&gt;&gt;&gt;&gt; @&gt;&gt;&gt;&gt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -