perlform.html

来自「perl教程」· HTML 代码 · 共 479 行 · 第 1/3 页

HTML
479
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlform - Perl formats</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlform - Perl formats</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#text_fields">Text Fields</a></li>
		<li><a href="#numeric_fields">Numeric Fields</a></li>
		<li><a href="#the_field____for_variable_width_multiline_text">The Field @* for Variable Width Multi-Line Text</a></li>
		<li><a href="#the_field____for_variable_width_onelineatatime_text">The Field ^* for Variable Width One-line-at-a-time Text</a></li>
		<li><a href="#specifying_values">Specifying Values</a></li>
		<li><a href="#using_fill_mode">Using Fill Mode</a></li>
		<li><a href="#suppressing_lines_where_all_fields_are_void">Suppressing Lines Where All Fields Are Void</a></li>
		<li><a href="#repeating_format_lines">Repeating Format Lines</a></li>
		<li><a href="#top_of_form_processing">Top of Form Processing</a></li>
		<li><a href="#format_variables">Format Variables</a></li>
	</ul>

	<li><a href="#notes">NOTES</a></li>
	<ul>

		<li><a href="#footers">Footers</a></li>
		<li><a href="#accessing_formatting_internals">Accessing Formatting Internals</a></li>
	</ul>

	<li><a href="#warnings">WARNINGS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlform - Perl formats</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Perl has a mechanism to help you generate simple reports and charts.  To
facilitate this, Perl helps you code up your output page close to how it
will look when it's printed.  It can keep track of things like how many
lines are on a page, what page you're on, when to print page headers,
etc.  Keywords are borrowed from FORTRAN: <a href="../../lib/Pod/perlfunc.html#item_format"><code>format()</code></a> to declare and <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a>
to execute; see their entries in <a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a>.  Fortunately, the layout is
much more legible, more like BASIC's PRINT USING statement.  Think of it
as a poor man's nroff(1).</p>
<p>Formats, like packages and subroutines, are declared rather than
executed, so they may occur at any point in your program.  (Usually it's
best to keep them all together though.) They have their own namespace
apart from all the other &quot;types&quot; in Perl.  This means that if you have a
function named &quot;Foo&quot;, it is not the same thing as having a format named
&quot;Foo&quot;.  However, the default name for the format associated with a given
filehandle is the same as the name of the filehandle.  Thus, the default
format for STDOUT is named &quot;STDOUT&quot;, and the default format for filehandle
TEMP is named &quot;TEMP&quot;.  They just look the same.  They aren't.</p>
<p>Output record formats are declared as follows:</p>
<pre>
    format NAME =
    FORMLIST
    .</pre>
<p>If the name is omitted, format &quot;STDOUT&quot; is defined. A single &quot;.&quot; in 
column 1 is used to terminate a format.  FORMLIST consists of a sequence 
of lines, each of which may be one of three types:</p>
<ol>
<li>
<p>A comment, indicated by putting a '#' in the first column.</p>
</li>
<li>
<p>A &quot;picture&quot; line giving the format for one output line.</p>
</li>
<li>
<p>An argument line supplying values to plug into the previous picture line.</p>
</li>
</ol>
<p>Picture lines contain output field definitions, intermingled with
literal text. These lines do not undergo any kind of variable interpolation.
Field definitions are made up from a set of characters, for starting and
extending a field to its desired width. This is the complete set of
characters for field definitions:

  &gt; &gt;  &gt;&gt;</p>
<pre>

   @    start of regular field
   ^    start of special field
   &lt;    pad character for left adjustification
   |    pad character for centering
   &gt;    pad character for right adjustificat
   #    pad character for a right justified numeric field
   0    instead of first #: pad number with leading zeroes
   .    decimal point within a numeric field
   ...  terminate a text field, show &quot;...&quot; as truncation evidence
   @*   variable width field for a multi-line value
   ^*   variable width field for next line of a multi-line value
   ~    suppress line with all fields empty
   ~~   repeat line until all fields are exhausted</pre>
<p>Each field in a picture line starts with either &quot;@&quot; (at) or &quot;^&quot; (caret),
indicating what we'll call, respectively, a &quot;regular&quot; or &quot;special&quot; field.
The choice of pad characters determines whether a field is textual or
numeric. The tilde operators are not part of a field.  Let's look at
the various possibilities in detail.</p>
<p>
</p>
<h2><a name="text_fields">Text Fields</a></h2>
<p>The length of the field is supplied by padding out the field with multiple 
&quot;&lt;&quot;, &quot;&gt;&quot;, or &quot;|&quot; characters to specify a non-numeric field with,
respectively, left justification, right justification, or centering. 
For a regular field, the value (up to the first newline) is taken and
printed according to the selected justification, truncating excess characters.
If you terminate a text field with &quot;...&quot;, three dots will be shown if
the value is truncated. A special text field may be used to do rudimentary 
multi-line text block filling; see <a href="#using_fill_mode">Using Fill Mode</a> for details.</p>
<pre>
   Example:
      format STDOUT =
      @&lt;&lt;&lt;&lt;&lt;&lt;   @||||||   @&gt;&gt;&gt;&gt;&gt;&gt;
      &quot;left&quot;,   &quot;middle&quot;, &quot;right&quot;
      .
   Output:
      left      middle    right</pre>
<p>
</p>
<h2><a name="numeric_fields">Numeric Fields</a></h2>
<p>Using &quot;#&quot; as a padding character specifies a numeric field, with
right justification. An optional &quot;.&quot; defines the position of the
decimal point. With a &quot;0&quot; (zero) instead of the first &quot;#&quot;, the
formatted number will be padded with leading zeroes if necessary.
A special numeric field is blanked out if the value is undefined.
If the resulting value would exceed the width specified the field is
filled with &quot;#&quot; as overflow evidence.</p>
<pre>
   Example:
      format STDOUT =
      @###   @.###   @##.###  @###   @###   ^####
       42,   3.1415,  undef,    0, 10000,   undef
      .
   Output:

⌨️ 快捷键说明

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