perlform.html
来自「perl教程」· HTML 代码 · 共 479 行 · 第 1/3 页
HTML
479 行
<span class="keyword">select</span><span class="operator">(</span><span class="variable">$ofh</span><span class="operator">);</span>
</pre>
<p>If you use the English module, you can even read the variable names:</p>
<pre>
<span class="keyword">use</span> <span class="variable">English</span> <span class="string">'-no_match_vars'</span><span class="operator">;</span>
<span class="variable">$ofh</span> <span class="operator">=</span> <span class="keyword">select</span><span class="operator">(</span><span class="variable">OUTF</span><span class="operator">);</span>
<span class="variable">$FORMAT_NAME</span> <span class="operator">=</span> <span class="string">"My_Other_Format"</span><span class="operator">;</span>
<span class="variable">$FORMAT_TOP_NAME</span> <span class="operator">=</span> <span class="string">"My_Top_Format"</span><span class="operator">;</span>
<span class="keyword">select</span><span class="operator">(</span><span class="variable">$ofh</span><span class="operator">);</span>
</pre>
<p>But you still have those funny select()s. So just use the FileHandle
module. Now, you can access these special variables using lowercase
method names instead:</p>
<pre>
<span class="keyword">use</span> <span class="variable">FileHandle</span><span class="operator">;</span>
<span class="variable">format_name</span> <span class="variable">OUTF</span> <span class="string">"My_Other_Format"</span><span class="operator">;</span>
<span class="variable">format_top_name</span> <span class="variable">OUTF</span> <span class="string">"My_Top_Format"</span><span class="operator">;</span>
</pre>
<p>Much better!</p>
<p>
</p>
<hr />
<h1><a name="notes">NOTES</a></h1>
<p>Because the values line may contain arbitrary expressions (for at fields,
not caret fields), you can farm out more sophisticated processing
to other functions, like <a href="../../lib/Pod/perlfunc.html#item_sprintf"><code>sprintf()</code></a> or one of your own. For example:</p>
<pre>
format Ident =
@<<<<<<<<<<<<<<<
&commify($n)
.</pre>
<p>To get a real at or caret into the field, do this:</p>
<pre>
format Ident =
I have an @ here.
"@"
.</pre>
<p>To center a whole line of text, do something like this:</p>
<pre>
format Ident =
@|||||||||||||||||||||||||||||||||||||||||||||||
"Some text line"
.</pre>
<p>There is no builtin way to say "float this to the right hand side
of the page, however wide it is." You have to specify where it goes.
The truly desperate can generate their own format on the fly, based
on the current number of columns, and then <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> it:</p>
<pre>
<span class="variable">$format</span> <span class="operator">=</span> <span class="string">"format STDOUT = \n"</span>
<span class="operator">.</span> <span class="string">'^'</span> <span class="operator">.</span> <span class="string">'<'</span> <span class="operator">x</span> <span class="variable">$cols</span> <span class="operator">.</span> <span class="string">"\n"</span>
<span class="operator">.</span> <span class="string">'$entry'</span> <span class="operator">.</span> <span class="string">"\n"</span>
<span class="operator">.</span> <span class="string">"\t^"</span> <span class="operator">.</span> <span class="string">"<"</span> <span class="operator">x</span> <span class="operator">(</span><span class="variable">$cols</span><span class="operator">-</span><span class="number">8</span><span class="operator">)</span> <span class="operator">.</span> <span class="string">"~~\n"</span>
<span class="operator">.</span> <span class="string">'$entry'</span> <span class="operator">.</span> <span class="string">"\n"</span>
<span class="operator">.</span> <span class="string">".\n"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">$format</span> <span class="keyword">if</span> <span class="variable">$Debugging</span><span class="operator">;</span>
<span class="keyword">eval</span> <span class="variable">$format</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="variable">$@</span> <span class="keyword">if</span> <span class="variable">$@</span><span class="operator">;</span>
</pre>
<p>Which would generate a format looking something like this:</p>
<pre>
format STDOUT =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$entry
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$entry
.</pre>
<p>Here's a little program that's somewhat like fmt(1):</p>
<pre>
format =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
$_</pre>
<pre>
.</pre>
<pre>
<span class="variable">$/</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="regex">s/\s*\n\s*/ /g</span><span class="operator">;</span>
<span class="keyword">write</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="footers">Footers</a></h2>
<p>While $FORMAT_TOP_NAME contains the name of the current header format,
there is no corresponding mechanism to automatically do the same thing
for a footer. Not knowing how big a format is going to be until you
evaluate it is one of the major problems. It's on the TODO list.</p>
<p>Here's one strategy: If you have a fixed-size footer, you can get footers
by checking $FORMAT_LINES_LEFT before each <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> and print the footer
yourself if necessary.</p>
<p>Here's another strategy: Open a pipe to yourself, using <a href="../../lib/Pod/perlfunc.html#item_open"><code>open(MYSELF, "|-")</code></a>
(see <a href="../../lib/Pod/perlfunc.html#open__">open() in the perlfunc manpage</a>) and always <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> to MYSELF instead of STDOUT.
Have your child process massage its STDIN to rearrange headers and footers
however you like. Not very convenient, but doable.</p>
<p>
</p>
<h2><a name="accessing_formatting_internals">Accessing Formatting Internals</a></h2>
<p>For low-level access to the formatting mechanism. you may use <a href="../../lib/Pod/perlfunc.html#item_formline"><code>formline()</code></a>
and access <a href="../../lib/Pod/perlvar.html#item___a"><code>$^A</code></a> (the $ACCUMULATOR variable) directly.</p>
<p>For example:</p>
<pre>
<span class="variable">$str</span> <span class="operator">=</span> <span class="keyword">formline</span> <span class="operator"><<</span><span class="default">'END'</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span><span class="operator">;</span><span class="string">
@<<< @||| @>>>
</span><span class="default">END</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"Wow, I just stored `$^A' in the accumulator!\n"</span><span class="operator">;</span>
</pre>
<p>Or to make an <code>swrite()</code> subroutine, which is to <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> what <a href="../../lib/Pod/perlfunc.html#item_sprintf"><code>sprintf()</code></a>
is to printf(), do this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Carp</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> swrite </span><span class="operator">{</span>
<span class="variable">croak</span> <span class="string">"usage: swrite PICTURE ARGS"</span> <span class="keyword">unless</span> <span class="variable">@_</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$format</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="variable">$^A</span> <span class="operator">=</span> <span class="string">""</span><span class="operator">;</span>
<span class="keyword">formline</span><span class="operator">(</span><span class="variable">$format</span><span class="operator">,</span><span class="variable">@_</span><span class="operator">);</span>
<span class="keyword">return</span> <span class="variable">$^A</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$string</span> <span class="operator">=</span> <span class="variable">swrite</span><span class="operator">(<<</span><span class="default">'END'</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">);</span><span class="string">
Check me out
@<<< @||| @>>>
END
print $string;
</span>
</pre>
<p>
</p>
<hr />
<h1><a name="warnings">WARNINGS</a></h1>
<p>The lone dot that ends a format can also prematurely end a mail
message passing through a misconfigured Internet mailer (and based on
experience, such misconfiguration is the rule, not the exception). So
when sending format code through mail, you should indent it so that
the format-ending dot is not on the left margin; this will prevent
SMTP cutoff.</p>
<p>Lexical variables (declared with "my") are not visible within a
format unless the format is declared within the scope of the lexical
variable. (They weren't visible at all before version 5.001.)</p>
<p>Formats are the only part of Perl that unconditionally use information
from a program's locale; if a program's environment specifies an
LC_NUMERIC locale, it is always used to specify the decimal point
character in formatted output. Perl ignores all other aspects of locale
handling unless the <code>use locale</code> pragma is in effect. Formatted output
cannot be controlled by <code>use locale</code> because the pragma is tied to the
block structure of the program, and, for historical reasons, formats
exist outside that block structure. See <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a> for further
discussion of locale handling.</p>
<p>Within strings that are to be displayed in a fixed length text field,
each control character is substituted by a space. (But remember the
special meaning of <code>\r</code> when using fill mode.) This is done to avoid
misalignment when control characters "disappear" on some output media.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?