perlform.html
来自「perl教程」· HTML 代码 · 共 479 行 · 第 1/3 页
HTML
479 行
42 3.142 0.000 0 ####</pre>
<p>
</p>
<h2><a name="the_field____for_variable_width_multiline_text">The Field @* for Variable Width Multi-Line Text</a></h2>
<p>The field "@*" can be used for printing multi-line, nontruncated
values; it should (but need not) appear by itself on a line. A final
line feed is chomped off, but all other characters are emitted verbatim.</p>
<p>
</p>
<h2><a name="the_field____for_variable_width_onelineatatime_text">The Field ^* for Variable Width One-line-at-a-time Text</a></h2>
<p>Like "@*", this is a variable width field. The value supplied must be a
scalar variable. Perl puts the first line (up to the first "\n") of the
text into the field, and then chops off the front of the string so that
the next time the variable is referenced, more of the text can be printed.
The variable will <em>not</em> be restored.</p>
<pre>
<span class="variable">Example</span><span class="operator">:</span>
<span class="variable">$text</span> <span class="operator">=</span> <span class="string">"line 1\nline 2\nline 3"</span><span class="operator">;</span>
<span class="keyword">format</span> <span class="variable">STDOUT</span> <span class="operator">=</span><span class="default">
Text: ^*
$text
~~ ^*
$text
.
Output:
Text: line 1
line 2
line 3
</span>
</pre>
<p>
</p>
<h2><a name="specifying_values">Specifying Values</a></h2>
<p>The values are specified on the following format line in the same order as
the picture fields. The expressions providing the values must be
separated by commas. They are all evaluated in a list context
before the line is processed, so a single list expression could produce
multiple list elements. The expressions may be spread out to more than
one line if enclosed in braces. If so, the opening brace must be the first
token on the first line. If an expression evaluates to a number with a
decimal part, and if the corresponding picture specifies that the decimal
part should appear in the output (that is, any picture except multiple "#"
characters <strong>without</strong> an embedded "."), the character used for the decimal
point is <strong>always</strong> determined by the current LC_NUMERIC locale. This
means that, if, for example, the run-time environment happens to specify a
German locale, "," will be used instead of the default ".". See
<a href="../../lib/Pod/perllocale.html">the perllocale manpage</a> and <a href="#warnings">WARNINGS</a> for more information.</p>
<p>
</p>
<h2><a name="using_fill_mode">Using Fill Mode</a></h2>
<p>On text fields the caret enables a kind of fill mode. Instead of an
arbitrary expression, the value supplied must be a scalar variable
that contains a text string. Perl puts the next portion of the text into
the field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. (Yes, this
means that the variable itself is altered during execution of the <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a>
call, and is not restored.) The next portion of text is determined by
a crude line breaking algorithm. You may use the carriage return character
(<code>\r</code>) to force a line break. You can change which characters are legal
to break on by changing the variable <a href="../../lib/Pod/perlvar.html#item___"><code>$:</code></a> (that's
$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
list of the desired characters.</p>
<p>Normally you would use a sequence of fields in a vertical stack associated
with the same scalar variable to print out a block of text. You might wish
to end the final field with the text "...", which will appear in the output
if the text was too long to appear in its entirety.</p>
<p>
</p>
<h2><a name="suppressing_lines_where_all_fields_are_void">Suppressing Lines Where All Fields Are Void</a></h2>
<p>Using caret fields can produce lines where all fields are blank. You can
suppress such lines by putting a "~" (tilde) character anywhere in the
line. The tilde will be translated to a space upon output.</p>
<p>
</p>
<h2><a name="repeating_format_lines">Repeating Format Lines</a></h2>
<p>If you put two contiguous tilde characters "~~" anywhere into a line,
the line will be repeated until all the fields on the line are exhausted,
i.e. undefined. For special (caret) text fields this will occur sooner or
later, but if you use a text field of the at variety, the expression you
supply had better not give the same value every time forever! (<a href="../../lib/Pod/perlfunc.html#item_shift"><code>shift(@f)</code></a>
is a simple example that would work.) Don't use a regular (at) numeric
field in such lines, because it will never go blank.</p>
<p>
</p>
<h2><a name="top_of_form_processing">Top of Form Processing</a></h2>
<p>Top-of-form processing is by default handled by a format with the
same name as the current filehandle with "_TOP" concatenated to it.
It's triggered at the top of each page. See <a href="../../lib/Pod/perlfunc.html#write">write in the perlfunc manpage</a>.</p>
<p>Examples:</p>
<pre>
# a report on the /etc/passwd file
format STDOUT_TOP =
Passwd File
Name Login Office Uid Gid Home
------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
$name, $login, $office,$uid,$gid, $home
.</pre>
<pre>
# a report from a bug report form
format STDOUT_TOP =
Bug Reports
@<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>
$system, $%, $date
------------------------------------------------------------------
.
format STDOUT =
Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$subject
Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$index, $description
Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$priority, $date, $description
From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$from, $description
Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$programmer, $description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<...
$description
.</pre>
<p>It is possible to intermix print()s with write()s on the same output
channel, but you'll have to handle <a href="../../lib/Pod/perlvar.html#item__"><code>$-</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_lines_left"><code>$FORMAT_LINES_LEFT</code></a>)
yourself.</p>
<p>
</p>
<h2><a name="format_variables">Format Variables</a></h2>
<p>The current format name is stored in the variable <a href="../../lib/Pod/perlvar.html#item___"><code>$~</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_name"><code>$FORMAT_NAME</code></a>),
and the current top of form format name is in <a href="../../lib/Pod/perlvar.html#item___"><code>$^</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_top_name"><code>$FORMAT_TOP_NAME</code></a>).
The current output page number is stored in <a href="../../lib/Pod/perlvar.html#item___"><code>$%</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_page_number"><code>$FORMAT_PAGE_NUMBER</code></a>),
and the number of lines on the page is in <a href="../../lib/Pod/perlvar.html#item___"><code>$=</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_lines_per_page"><code>$FORMAT_LINES_PER_PAGE</code></a>).
Whether to autoflush output on this handle is stored in <a href="../../lib/Pod/perlvar.html#item___"><code>$|</code></a>
(<a href="../../lib/Pod/perlvar.html#item__output_autoflush"><code>$OUTPUT_AUTOFLUSH</code></a>). The string output before each top of page (except
the first) is stored in <a href="../../lib/Pod/perlvar.html#item___l"><code>$^L</code></a> (<a href="../../lib/Pod/perlvar.html#item__format_formfeed"><code>$FORMAT_FORMFEED</code></a>). These variables are
set on a per-filehandle basis, so you'll need to <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> into a different
one to affect them:</p>
<pre>
<span class="keyword">select</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">$~</span> <span class="operator">=</span> <span class="string">"My_Other_Format"</span><span class="operator">,</span>
<span class="variable">$^</span> <span class="operator">=</span> <span class="string">"My_Top_Format"</span>
<span class="operator">)</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<p>Pretty ugly, eh? It's a common idiom though, so don't be too surprised
when you see it. You can at least use a temporary variable to hold
the previous filehandle: (this is a much better approach in general,
because not only does legibility improve, you now have intermediary
stage in the expression to single-step the debugger through):</p>
<pre>
<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">$~</span> <span class="operator">=</span> <span class="string">"My_Other_Format"</span><span class="operator">;</span>
<span class="variable">$^</span> <span class="operator">=</span> <span class="string">"My_Top_Format"</span><span class="operator">;</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?