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

📄 ch07_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
$name,              $login,  $office,$uid,$gid, $home.# a report from a bug report formformat STDOUT_TOP =                         Bug Reports@&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;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;$system,                      $%,         $date------------------------------------------------------------------.format STDOUT =Subject: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;         $subjectIndex: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;       $index,                       $descriptionPriority: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Date: @&lt;&lt;&lt;&lt;&lt;&lt;&lt; ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;          $priority,        $date,   $descriptionFrom: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;      $from,                         $descriptionAssigned to: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;             $programmer,            $description~                                    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                                     $description~                                    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                                     $description~                                    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                                     $description~                                    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                                     $description~                                    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;...                                     $description.</pre></blockquote><a name="INDEX-1908"></a></p><p><a name="INDEX-1909"></a>Lexical variables are not visible within a format unless the format isdeclared within the scope of the lexical variable.</p><p><a name="INDEX-1910"></a>It is possible to intermix <tt class="literal">print</tt>s with <tt class="literal">write</tt>s on the same outputchannel, but you'll have to handle the <tt class="literal">$-</tt> special variable(<tt class="literal">$FORMAT_LINES_LEFT</tt> if you're using the <tt class="literal">English</tt> module) yourself.</p><h2 class="sect1">7.1. Format Variables</h2><a name="INDEX-1911"></a><a name="INDEX-1912"></a><a name="INDEX-1913"></a><a name="INDEX-1914"></a><a name="INDEX-1915"></a><a name="INDEX-1916"></a><a name="INDEX-1917"></a><a name="INDEX-1918"></a><a name="INDEX-1919"></a><p><a name="INDEX-1920"></a>The current format name is stored in the variable<tt class="literal">$~</tt> (<tt class="literal">$FORMAT_NAME</tt>), and thecurrent top-of-form format name is in <tt class="literal">$^</tt>(<tt class="literal">$FORMAT_TOP_NAME</tt>).  The current output page numberis stored in <tt class="literal">$%</tt>(<tt class="literal">$FORMAT_PAGE_NUMBER</tt>), and the number of lines onthe page is in <tt class="literal">$=</tt>(<tt class="literal">$FORMAT_LINES_PER_PAGE</tt>). Whether to flush theoutput buffer on this handle automatically is stored in<tt class="literal">$|</tt> (<tt class="literal">$OUTPUT_AUTOFLUSH</tt>).  Thestring to be output before each top of page (except the first) isstored in <tt class="literal">$^L</tt>(<tt class="literal">$FORMAT_FORMFEED</tt>).  These variables are set on aper-filehandle basis, so you'll need to <tt class="literal">select</tt> thefilehandle associated with a format in order to affect its formatvariables:<blockquote><pre class="programlisting">select((select(OUTF),         $~ = "My_Other_Format",        $^ = "My_Top_Format"       )[0]);</pre></blockquote>Pretty ugly, eh?  It's a common idiom though, so don't be too surprisedwhen you see it.  You can at least use a temporary variable to hold theprevious filehandle:<blockquote><pre class="programlisting">$ofh = select(OUTF);$~ = "My_Other_Format";$^ = "My_Top_Format";select($ofh);</pre></blockquote><a name="INDEX-1921"></a><a name="INDEX-1922"></a></p><p>This is a much better approach in general becausenot only does legibility improve, but you now have an intermediarystatement in the code to stop on when you're single-stepping in thedebugger. If you use the <tt class="literal">English</tt> module, you can even read the variable names:</p><blockquote><pre class="programlisting">use English;$ofh = select(OUTF);$FORMAT_NAME     = "My_Other_Format";$FORMAT_TOP_NAME = "My_Top_Format";select($ofh);</pre></blockquote><p>But you still have those funny calls to <tt class="literal">select</tt>.  If you want toavoid them, use the <tt class="literal">FileHandle</tt> module bundled with Perl.  Now youcan access these special variables using lowercase method namesinstead:<blockquote><pre class="programlisting">use FileHandle;OUTF-&gt;format_name("My_Other_Format");OUTF-&gt;format_top_name("My_Top_Format");</pre></blockquote>Much better!</p><p><a name="INDEX-1923"></a>Since the values line following your picture line may contain arbitraryexpressions (for <tt class="literal">@</tt> fields, not <tt class="literal">^</tt> fields), you can farm out moresophisticated processing to other functions, like <tt class="literal">sprintf</tt> or one ofyour own.  For example, to insert commas into a number:<blockquote><pre class="programlisting">format Ident =     @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;    commify($n).</pre></blockquote>To get a real <tt class="literal">@</tt>, <tt class="literal">~</tt>, or <tt class="literal">^</tt> into the field, do this:<blockquote><pre class="programlisting">format Ident = I have an @ here.         "@".</pre></blockquote><a name="INDEX-1924"></a><a name="INDEX-1925"></a></p><p>To center a whole line of text, do something like this:<blockquote><pre class="programlisting">format Ident = @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||                          "Some text line".</pre></blockquote><a name="INDEX-1926"></a><a name="INDEX-1927"></a><a name="INDEX-1928"></a><a name="INDEX-1929"></a><a name="INDEX-1930"></a></p><p>The <tt class="literal">&gt;</tt> field-length indicator ensures that the text will beright-justified within the field, but the field as a whole occursexactly where you show it occurring. There is no built-in way to say"float this field to the right-hand side of the page, however wide itis."  You have to specify where it goes relative to the left margin. Thetruly desperate can generate their own format on the fly, based on thecurrent number of columns (not supplied), and then <tt class="literal">eval</tt> it:<blockquote><pre class="programlisting">$format  = "format STDOUT = \n"         . '^' . '&lt;' x $cols . "\n"         . '$entry' . "\n"         . "\t^" . "&lt;" x ($cols-8) . "~~\n"         . '$entry' . "\n"         . ".\n";print $format if $Debugging;eval $format; die $@ if $@;</pre></blockquote>The most important line there is probably the <tt class="literal">print</tt>. What the<tt class="literal">print</tt> would print out looks something like this:<blockquote><pre class="programlisting">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;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;$entry    ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;~~$entry.</pre></blockquote><a name="INDEX-1931"></a></p><p><a name="INDEX-1932"></a>Here's a little program that behaves like the <em class="emphasis">fmt</em>(1) Unixutility:</p><blockquote><pre class="programlisting">format = ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ~~$_.$/ = "";while (&lt;&gt;) {    s/\s*\n\s*/ /g;    write;}</pre></blockquote><a name="INDEX-1944"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><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="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></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><tr><td align="left" valign="top" width="172">6.5. Subroutine Attributes</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">7.2. Footers</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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