📄 ch04_12.htm
字号:
<html><head><title>Formats (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_11.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_13.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.12. Formats</h2><p><a name="INDEX-794" /><a name="INDEX-795" /><a name="INDEX-796" /><a name="INDEX-797" />Formats are a mechanism for generatingformatted reports for outputting data and are defined with the<tt class="literal">format</tt> keyword. The general form looks like:</p><blockquote><pre class="code">format <em class="replaceable"><tt>name</tt></em> = <em class="replaceable"><tt>...template lines..</tt></em>. <em class="replaceable"><tt>...argument line..</tt></em>..</pre></blockquote><p>Most of your format names will be the same as the filehandle namesfor which they are used. The default format for a filehandle is theone with the same name.</p><p>The format definition is like a subroutine definition. Itdoesn't contain immediately executed code and cantherefore be placed anywhere in the file with the rest of theprogram; they are commonly placed near the end of the file withsubroutine definitions<a name="INDEX-798" />. To output to a format,use the <tt class="literal">write</tt> function instead of<tt class="literal">print</tt>.</p><p><a name="INDEX-799" />The templatelines contain literal text and fieldholders. Fieldholders containsymbols that describe the size and positioning of the area on theline where data is output. An argument line immediately follows atemplate line that contains the fields to be replaced by data. Theargument line is a list of variables (or expressions), separated bycommas, that fill the fields in the previous line in the order theyare listed.</p><p>Here's an example of a template line with twofieldholders and the argument line that follows:</p><blockquote><pre class="code">Hello, my name is @<<<<<<<<<< and I'm @<< years old.$name, $age</pre></blockquote><p>The fieldholders are<tt class="literal">@<<<<<<<<<<</tt> and<tt class="literal">@<<</tt>, which specify left-justified textfields with 11 and 3 characters, respectively.</p><p><a name="INDEX-800" /><a name="INDEX-801" />Most fieldholders start with<tt class="literal">@</tt>. The characters following <tt class="literal">@</tt>indicate the type of field, while the number of characters (including<tt class="literal">@</tt>) indicate the field width. The followingfieldholder characters determine the positioning of text fields:</p><dl><dt><b><tt class="literal"><<<<</tt> <em class="emphasis">(left angle brackets)</em></b></dt><dd>A left-justified field; if the value is shorter than the field width,it will be padded on the right with spaces.</p></dd><dt><b><tt class="literal">>>>></tt> <em class="emphasis">(right angle brackets)</em></b></dt><dd>A right-justified field; if the value is too short, it will be paddedon the left with spaces.</p></dd><dt><b><tt class="literal">||||</tt> <em class="emphasis">(vertical bars)</em></b></dt><dd><a name="INDEX-802" /><a name="INDEX-803" />A centered field; if the value istoo short, it will be padded on both sides with spaces, enough oneach side to center the value within the field.</p></dd></dl><p><a name="INDEX-804" /><a name="INDEX-805" /><a name="INDEX-806" /><a name="INDEX-807" />Another kind of fieldholder is a<em class="emphasis">fixed-precision numeric field</em>. This field alsobegins with <tt class="literal">@</tt>, and is followed by one or morehashmarks (<tt class="literal">###</tt>) with an optional dot (indicating adecimal point). For example:</p><blockquote><pre class="code">format MONEY =Assets: @#####.## Liabilities: @#####.## Net: @#####.##$assets, $liabilities, $assets-$liabilities.</pre></blockquote><p><a name="INDEX-808" />Themultiline fieldholder allows you to include a value that may havemany lines of information. This fieldholder is denoted by<tt class="literal">@*</tt> on a line by itself. The next line defines thevalue that will be substituted into the field, which in this case maybe an expression that results in a value that contains many newlines.</p><p><a name="INDEX-809" />Another kind of fieldholder is a<em class="emphasis">filled field</em>. This fieldholder allows you tocreate a filled paragraph, breaking the text into conveniently sizedlines at word boundaries, wrapping the lines as needed. A filledfield is denoted by replacing the <tt class="literal">@</tt> marker in atext fieldholder with a caret (<tt class="literal">^<<<</tt>, forexample). The corresponding value for a filled field (on thefollowing line of the format) must be a scalar variable containingtext, rather than an expression that returns a scalar value. WhenPerl is filling the filled field, it takes the value of the variableand removes as many words as will fit in the field. Subsequent callsfor the variable in a filled field will continue where the last oneleft off.</p><p><a name="INDEX-810" /><a name="INDEX-811" /><a name="INDEX-812" />If the variable'scontents are exhausted before the number of fields, you will simplyend up with blank lines. You can suppress blank lines by placing atilde (<tt class="literal">~</tt>) on the line. Any line that contains atilde character is not output if the line would have otherwiseprinted blank (i.e., just whitespace). The tilde itself always printsas a blank and can be placed anywhere a space could have been placedin the line.</p><p>If the text in the variable is longer than what can be filled in thefields, output continues only until the fields runout<a name="INDEX-813" /><a name="INDEX-814" />. The shortcut to get thestring to print until it ends is to use two consecutive tildes(<tt class="literal">~~</tt>) on a line. This causes the line to repeatautomatically until the result is a completely blank line (which willbe suppressed).</p><p>Default values for format parameters all relate to the format of thecurrently selected filehandle. The currently selected filehandlestarts out as STDOUT, which makes it easy to print things on thestandard output. However, you can change the currently selectedfilehandle with the <tt class="literal">select</tt> function, which takes asingle filehandle (or a scalar variable containing the name of afilehandle) as an argument. Once the currently selected filehandle ischanged, it affects all future operations that depend on thecurrently selected filehandle. <a name="INDEX-815" /><a name="INDEX-816" /><a name="INDEX-817" /> </p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_11.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch04_13.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">4.11. Unicode</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">4.13. Pod</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -