Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perlform.html
blobf6e101c667a395e69a5468c7b9938c1dc0a651ec
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perlform - Perl formats</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perlform - Perl formats</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#text_fields">Text Fields</a></li>
27 <li><a href="#numeric_fields">Numeric Fields</a></li>
28 <li><a href="#the_field____for_variable_width_multiline_text">The Field @* for Variable Width Multi-Line Text</a></li>
29 <li><a href="#the_field____for_variable_width_onelineatatime_text">The Field ^* for Variable Width One-line-at-a-time Text</a></li>
30 <li><a href="#specifying_values">Specifying Values</a></li>
31 <li><a href="#using_fill_mode">Using Fill Mode</a></li>
32 <li><a href="#suppressing_lines_where_all_fields_are_void">Suppressing Lines Where All Fields Are Void</a></li>
33 <li><a href="#repeating_format_lines">Repeating Format Lines</a></li>
34 <li><a href="#top_of_form_processing">Top of Form Processing</a></li>
35 <li><a href="#format_variables">Format Variables</a></li>
36 </ul>
38 <li><a href="#notes">NOTES</a></li>
39 <ul>
41 <li><a href="#footers">Footers</a></li>
42 <li><a href="#accessing_formatting_internals">Accessing Formatting Internals</a></li>
43 </ul>
45 <li><a href="#warnings">WARNINGS</a></li>
46 </ul>
47 <!-- INDEX END -->
49 <hr />
50 <p>
51 </p>
52 <h1><a name="name_x_format__x_report__x_chart_">NAME
53 </a></h1>
54 <p>perlform - Perl formats</p>
55 <p>
56 </p>
57 <hr />
58 <h1><a name="description">DESCRIPTION</a></h1>
59 <p>Perl has a mechanism to help you generate simple reports and charts. To
60 facilitate this, Perl helps you code up your output page close to how it
61 will look when it's printed. It can keep track of things like how many
62 lines are on a page, what page you're on, when to print page headers,
63 etc. Keywords are borrowed from FORTRAN: <code>format()</code> to declare and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()</code></a>
64 to execute; see their entries in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html">the perlfunc manpage</a>. Fortunately, the layout is
65 much more legible, more like BASIC's PRINT USING statement. Think of it
66 as a poor man's nroff(1).
67 </p>
68 <p>Formats, like packages and subroutines, are declared rather than
69 executed, so they may occur at any point in your program. (Usually it's
70 best to keep them all together though.) They have their own namespace
71 apart from all the other ``types'' in Perl. This means that if you have a
72 function named ``Foo'', it is not the same thing as having a format named
73 ``Foo''. However, the default name for the format associated with a given
74 filehandle is the same as the name of the filehandle. Thus, the default
75 format for STDOUT is named ``STDOUT'', and the default format for filehandle
76 TEMP is named ``TEMP''. They just look the same. They aren't.</p>
77 <p>Output record formats are declared as follows:</p>
78 <pre>
79 format NAME =
80 FORMLIST
81 .</pre>
82 <p>If the name is omitted, format ``STDOUT'' is defined. A single ``.'' in
83 column 1 is used to terminate a format. FORMLIST consists of a sequence
84 of lines, each of which may be one of three types:</p>
85 <ol>
86 <li>
87 <p>A comment, indicated by putting a '#' in the first column.</p>
88 </li>
89 <li>
90 <p>A ``picture'' line giving the format for one output line.</p>
91 </li>
92 <li>
93 <p>An argument line supplying values to plug into the previous picture line.</p>
94 </li>
95 </ol>
96 <p>Picture lines contain output field definitions, intermingled with
97 literal text. These lines do not undergo any kind of variable interpolation.
98 Field definitions are made up from a set of characters, for starting and
99 extending a field to its desired width. This is the complete set of
100 characters for field definitions:
102 &gt; &gt; &gt;&gt;
104 </p>
105 <pre>
107 @ start of regular field
108 ^ start of special field
109 &lt; pad character for left adjustification
110 | pad character for centering
111 &gt; pad character for right adjustificat
112 # pad character for a right justified numeric field
113 0 instead of first #: pad number with leading zeroes
114 . decimal point within a numeric field
115 ... terminate a text field, show &quot;...&quot; as truncation evidence
116 @* variable width field for a multi-line value
117 ^* variable width field for next line of a multi-line value
118 ~ suppress line with all fields empty
119 ~~ repeat line until all fields are exhausted</pre>
120 <p>Each field in a picture line starts with either ``@'' (at) or ``^'' (caret),
121 indicating what we'll call, respectively, a ``regular'' or ``special'' field.
122 The choice of pad characters determines whether a field is textual or
123 numeric. The tilde operators are not part of a field. Let's look at
124 the various possibilities in detail.</p>
126 </p>
127 <h2><a name="text_fields_x_format__text_field_">Text Fields
128 </a></h2>
129 <p>The length of the field is supplied by padding out the field with multiple
130 ``&lt;'', ``&gt;'', or ``|'' characters to specify a non-numeric field with,
131 respectively, left justification, right justification, or centering.
132 For a regular field, the value (up to the first newline) is taken and
133 printed according to the selected justification, truncating excess characters.
134 If you terminate a text field with ``...'', three dots will be shown if
135 the value is truncated. A special text field may be used to do rudimentary
136 multi-line text block filling; see <a href="#using_fill_mode">Using Fill Mode</a> for details.</p>
137 <pre>
138 Example:
139 format STDOUT =
140 @&lt;&lt;&lt;&lt;&lt;&lt; @|||||| @&gt;&gt;&gt;&gt;&gt;&gt;
141 &quot;left&quot;, &quot;middle&quot;, &quot;right&quot;
143 Output:
144 left middle right</pre>
146 </p>
147 <h2><a name="numeric_fields_x____x_format__numeric_field_">Numeric Fields
148 </a></h2>
149 <p>Using ``#'' as a padding character specifies a numeric field, with
150 right justification. An optional ``.'' defines the position of the
151 decimal point. With a ``0'' (zero) instead of the first ``#'', the
152 formatted number will be padded with leading zeroes if necessary.
153 A special numeric field is blanked out if the value is undefined.
154 If the resulting value would exceed the width specified the field is
155 filled with ``#'' as overflow evidence.</p>
156 <pre>
157 Example:
158 format STDOUT =
159 @### @.### @##.### @### @### ^####
160 42, 3.1415, undef, 0, 10000, undef
162 Output:
163 42 3.142 0.000 0 ####</pre>
165 </p>
166 <h2><a name="the_field____for_variable_width_multiline_text_x____">The Field @* for Variable Width Multi-Line Text
167 </a></h2>
168 <p>The field ``@*'' can be used for printing multi-line, nontruncated
169 values; it should (but need not) appear by itself on a line. A final
170 line feed is chomped off, but all other characters are emitted verbatim.</p>
172 </p>
173 <h2><a name="the_field____for_variable_width_onelineatatime_text_x____">The Field ^* for Variable Width One-line-at-a-time Text
174 </a></h2>
175 <p>Like ``@*'', this is a variable width field. The value supplied must be a
176 scalar variable. Perl puts the first line (up to the first ``\n'') of the
177 text into the field, and then chops off the front of the string so that
178 the next time the variable is referenced, more of the text can be printed.
179 The variable will <em>not</em> be restored.</p>
180 <pre>
181 Example:
182 $text = &quot;line 1\nline 2\nline 3&quot;;
183 format STDOUT =
184 Text: ^*
185 $text
186 ~~ ^*
187 $text
189 Output:
190 Text: line 1
191 line 2
192 line 3</pre>
194 </p>
195 <h2><a name="specifying_values_x_format__specifying_values_">Specifying Values
196 </a></h2>
197 <p>The values are specified on the following format line in the same order as
198 the picture fields. The expressions providing the values must be
199 separated by commas. They are all evaluated in a list context
200 before the line is processed, so a single list expression could produce
201 multiple list elements. The expressions may be spread out to more than
202 one line if enclosed in braces. If so, the opening brace must be the first
203 token on the first line. If an expression evaluates to a number with a
204 decimal part, and if the corresponding picture specifies that the decimal
205 part should appear in the output (that is, any picture except multiple ``#''
206 characters <strong>without</strong> an embedded ``.''), the character used for the decimal
207 point is <strong>always</strong> determined by the current LC_NUMERIC locale. This
208 means that, if, for example, the run-time environment happens to specify a
209 German locale, ``,'' will be used instead of the default ``.''. See
210 <a href="file://C|\msysgit\mingw\html/pod/perllocale.html">the perllocale manpage</a> and <a href="#warnings">WARNINGS</a> for more information.</p>
212 </p>
213 <h2><a name="using_fill_mode_x_format__fill_mode_">Using Fill Mode
214 </a></h2>
215 <p>On text fields the caret enables a kind of fill mode. Instead of an
216 arbitrary expression, the value supplied must be a scalar variable
217 that contains a text string. Perl puts the next portion of the text into
218 the field, and then chops off the front of the string so that the next time
219 the variable is referenced, more of the text can be printed. (Yes, this
220 means that the variable itself is altered during execution of the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()</code></a>
221 call, and is not restored.) The next portion of text is determined by
222 a crude line breaking algorithm. You may use the carriage return character
223 (<code>\r</code>) to force a line break. You can change which characters are legal
224 to break on by changing the variable <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$:</code></a> (that's
225 $FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
226 list of the desired characters.</p>
227 <p>Normally you would use a sequence of fields in a vertical stack associated
228 with the same scalar variable to print out a block of text. You might wish
229 to end the final field with the text ``...'', which will appear in the output
230 if the text was too long to appear in its entirety.</p>
232 </p>
233 <h2><a name="suppressing_lines_where_all_fields_are_void_x_format__suppressing_lines_">Suppressing Lines Where All Fields Are Void
234 </a></h2>
235 <p>Using caret fields can produce lines where all fields are blank. You can
236 suppress such lines by putting a ``~'' (tilde) character anywhere in the
237 line. The tilde will be translated to a space upon output.</p>
239 </p>
240 <h2><a name="repeating_format_lines_x_format__repeating_lines_">Repeating Format Lines
241 </a></h2>
242 <p>If you put two contiguous tilde characters ``~~'' anywhere into a line,
243 the line will be repeated until all the fields on the line are exhausted,
244 i.e. undefined. For special (caret) text fields this will occur sooner or
245 later, but if you use a text field of the at variety, the expression you
246 supply had better not give the same value every time forever! (<a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_shift"><code>shift(@f)</code></a>
247 is a simple example that would work.) Don't use a regular (at) numeric
248 field in such lines, because it will never go blank.</p>
250 </p>
251 <h2><a name="top_of_form_processing_x_format__top_of_form__x_top__x_header_">Top of Form Processing
252 </a></h2>
253 <p>Top-of-form processing is by default handled by a format with the
254 same name as the current filehandle with ``_TOP'' concatenated to it.
255 It's triggered at the top of each page. See <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write">write in the perlfunc manpage</a>.</p>
256 <p>Examples:</p>
257 <pre>
258 # a report on the /etc/passwd file
259 format STDOUT_TOP =
260 Passwd File
261 Name Login Office Uid Gid Home
262 ------------------------------------------------------------------
264 format STDOUT =
265 @&lt;&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; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
266 $name, $login, $office,$uid,$gid, $home
267 .</pre>
268 <pre>
269 # a report from a bug report form
270 format STDOUT_TOP =
271 Bug Reports
272 @&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;
273 $system, $%, $date
274 ------------------------------------------------------------------
276 format STDOUT =
277 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;
278 $subject
279 Index: @&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;
280 $index, $description
281 Priority: @&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;
282 $priority, $date, $description
283 From: @&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;
284 $from, $description
285 Assigned 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;
286 $programmer, $description
287 ~ ^&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;
288 $description
289 ~ ^&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;
290 $description
291 ~ ^&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;
292 $description
293 ~ ^&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;
294 $description
295 ~ ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;...
296 $description
297 .</pre>
298 <p>It is possible to intermix print()s with write()s on the same output
299 channel, but you'll have to handle <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__"><code>$-</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_lines_left"><code>$FORMAT_LINES_LEFT</code></a>)
300 yourself.</p>
302 </p>
303 <h2><a name="format_variables_x_format_variables__x_format__variables_">Format Variables
305 </a></h2>
306 <p>The current format name is stored in the variable <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$~</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_name"><code>$FORMAT_NAME</code></a>),
307 and the current top of form format name is in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$^</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_top_name"><code>$FORMAT_TOP_NAME</code></a>).
308 The current output page number is stored in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$%</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_page_number"><code>$FORMAT_PAGE_NUMBER</code></a>),
309 and the number of lines on the page is in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$=</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_lines_per_page"><code>$FORMAT_LINES_PER_PAGE</code></a>).
310 Whether to autoflush output on this handle is stored in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$|</code></a>
311 (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__output_autoflush"><code>$OUTPUT_AUTOFLUSH</code></a>). The string output before each top of page (except
312 the first) is stored in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___l"><code>$^L</code></a> (<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__format_formfeed"><code>$FORMAT_FORMFEED</code></a>). These variables are
313 set on a per-filehandle basis, so you'll need to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select"><code>select()</code></a> into a different
314 one to affect them:</p>
315 <pre>
316 select((select(OUTF),
317 $~ = &quot;My_Other_Format&quot;,
318 $^ = &quot;My_Top_Format&quot;
319 )[0]);</pre>
320 <p>Pretty ugly, eh? It's a common idiom though, so don't be too surprised
321 when you see it. You can at least use a temporary variable to hold
322 the previous filehandle: (this is a much better approach in general,
323 because not only does legibility improve, you now have intermediary
324 stage in the expression to single-step the debugger through):</p>
325 <pre>
326 $ofh = select(OUTF);
327 $~ = &quot;My_Other_Format&quot;;
328 $^ = &quot;My_Top_Format&quot;;
329 select($ofh);</pre>
330 <p>If you use the English module, you can even read the variable names:</p>
331 <pre>
332 use English '-no_match_vars';
333 $ofh = select(OUTF);
334 $FORMAT_NAME = &quot;My_Other_Format&quot;;
335 $FORMAT_TOP_NAME = &quot;My_Top_Format&quot;;
336 select($ofh);</pre>
337 <p>But you still have those funny select()s. So just use the FileHandle
338 module. Now, you can access these special variables using lowercase
339 method names instead:</p>
340 <pre>
341 use FileHandle;
342 format_name OUTF &quot;My_Other_Format&quot;;
343 format_top_name OUTF &quot;My_Top_Format&quot;;</pre>
344 <p>Much better!</p>
346 </p>
347 <hr />
348 <h1><a name="notes">NOTES</a></h1>
349 <p>Because the values line may contain arbitrary expressions (for at fields,
350 not caret fields), you can farm out more sophisticated processing
351 to other functions, like <code>sprintf()</code> or one of your own. For example:</p>
352 <pre>
353 format Ident =
354 @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
355 &amp;commify($n)
356 .</pre>
357 <p>To get a real at or caret into the field, do this:</p>
358 <pre>
359 format Ident =
360 I have an @ here.
361 &quot;@&quot;
362 .</pre>
363 <p>To center a whole line of text, do something like this:</p>
364 <pre>
365 format Ident =
366 @|||||||||||||||||||||||||||||||||||||||||||||||
367 &quot;Some text line&quot;
368 .</pre>
369 <p>There is no builtin way to say ``float this to the right hand side
370 of the page, however wide it is.'' You have to specify where it goes.
371 The truly desperate can generate their own format on the fly, based
372 on the current number of columns, and then <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a> it:</p>
373 <pre>
374 $format = &quot;format STDOUT = \n&quot;
375 . '^' . '&lt;' x $cols . &quot;\n&quot;
376 . '$entry' . &quot;\n&quot;
377 . &quot;\t^&quot; . &quot;&lt;&quot; x ($cols-8) . &quot;~~\n&quot;
378 . '$entry' . &quot;\n&quot;
379 . &quot;.\n&quot;;
380 print $format if $Debugging;
381 eval $format;
382 die $@ if $@;</pre>
383 <p>Which would generate a format looking something like this:</p>
384 <pre>
385 format STDOUT =
386 ^&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;
387 $entry
388 ^&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;~~
389 $entry
390 .</pre>
391 <p>Here's a little program that's somewhat like fmt(1):</p>
392 <pre>
393 format =
394 ^&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; ~~
395 $_</pre>
396 <pre>
397 .</pre>
398 <pre>
399 $/ = '';
400 while (&lt;&gt;) {
401 s/\s*\n\s*/ /g;
402 write;
403 }</pre>
405 </p>
406 <h2><a name="footers_x_format__footer__x_footer_">Footers
407 </a></h2>
408 <p>While $FORMAT_TOP_NAME contains the name of the current header format,
409 there is no corresponding mechanism to automatically do the same thing
410 for a footer. Not knowing how big a format is going to be until you
411 evaluate it is one of the major problems. It's on the TODO list.</p>
412 <p>Here's one strategy: If you have a fixed-size footer, you can get footers
413 by checking $FORMAT_LINES_LEFT before each <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()</code></a> and print the footer
414 yourself if necessary.</p>
415 <p>Here's another strategy: Open a pipe to yourself, using <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open(MYSELF, &quot;|-&quot;)</code></a>
416 (see <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open">open() in the perlfunc manpage</a>) and always <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()</code></a> to MYSELF instead of STDOUT.
417 Have your child process massage its STDIN to rearrange headers and footers
418 however you like. Not very convenient, but doable.</p>
420 </p>
421 <h2><a name="accessing_formatting_internals_x_format__internals_">Accessing Formatting Internals
422 </a></h2>
423 <p>For low-level access to the formatting mechanism. you may use <code>formline()</code>
424 and access <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___a"><code>$^A</code></a> (the $ACCUMULATOR variable) directly.</p>
425 <p>For example:</p>
426 <pre>
427 $str = formline &lt;&lt;'END', 1,2,3;
428 @&lt;&lt;&lt; @||| @&gt;&gt;&gt;
429 END</pre>
430 <pre>
431 print &quot;Wow, I just stored `$^A' in the accumulator!\n&quot;;</pre>
432 <p>Or to make an <code>swrite()</code> subroutine, which is to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()</code></a> what <code>sprintf()</code>
433 is to printf(), do this:</p>
434 <pre>
435 use Carp;
436 sub swrite {
437 croak &quot;usage: swrite PICTURE ARGS&quot; unless @_;
438 my $format = shift;
439 $^A = &quot;&quot;;
440 formline($format,@_);
441 return $^A;
442 }</pre>
443 <pre>
444 $string = swrite(&lt;&lt;'END', 1, 2, 3);
445 Check me out
446 @&lt;&lt;&lt; @||| @&gt;&gt;&gt;
448 print $string;</pre>
450 </p>
451 <hr />
452 <h1><a name="warnings">WARNINGS</a></h1>
453 <p>The lone dot that ends a format can also prematurely end a mail
454 message passing through a misconfigured Internet mailer (and based on
455 experience, such misconfiguration is the rule, not the exception). So
456 when sending format code through mail, you should indent it so that
457 the format-ending dot is not on the left margin; this will prevent
458 SMTP cutoff.</p>
459 <p>Lexical variables (declared with ``my'') are not visible within a
460 format unless the format is declared within the scope of the lexical
461 variable. (They weren't visible at all before version 5.001.)</p>
462 <p>Formats are the only part of Perl that unconditionally use information
463 from a program's locale; if a program's environment specifies an
464 LC_NUMERIC locale, it is always used to specify the decimal point
465 character in formatted output. Perl ignores all other aspects of locale
466 handling unless the <code>use locale</code> pragma is in effect. Formatted output
467 cannot be controlled by <code>use locale</code> because the pragma is tied to the
468 block structure of the program, and, for historical reasons, formats
469 exist outside that block structure. See <a href="file://C|\msysgit\mingw\html/pod/perllocale.html">the perllocale manpage</a> for further
470 discussion of locale handling.</p>
471 <p>Within strings that are to be displayed in a fixed length text field,
472 each control character is substituted by a space. (But remember the
473 special meaning of <code>\r</code> when using fill mode.) This is done to avoid
474 misalignment when control characters ``disappear'' on some output media.
476 </p>
477 <table border="0" width="100%" cellspacing="0" cellpadding="3">
478 <tr><td class="block" style="background-color: #cccccc" valign="middle">
479 <big><strong><span class="block">&nbsp;perlform - Perl formats</span></strong></big>
480 </td></tr>
481 </table>
483 </body>
485 </html>