Debian Codegraph @

upstream/mageia-cauldron/man1/perlform.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLFORM 1"
61 .TH PERLFORM 1 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlform \- Perl formats
68 .IX Xref "format report chart"
69 .SH DESCRIPTION
70 .IX Header "DESCRIPTION"
71 Perl has a mechanism to help you generate simple reports and charts. To
72 facilitate this, Perl helps you code up your output page close to how it
73 will look when it\*(Aqs printed. It can keep track of things like how many
74 lines are on a page, what page you\*(Aqre on, when to print page headers,
75 etc. Keywords are borrowed from FORTRAN: \fBformat()\fR to declare and \fBwrite()\fR
76 to execute; see their entries in perlfunc. Fortunately, the layout is
77 much more legible, more like BASIC\*(Aqs PRINT USING statement. Think of it
78 as a poor man\*(Aqs \fBnroff\fR\|(1).
79 .IX Xref "nroff"
80 .PP
81 Formats, like packages and subroutines, are declared rather than
82 executed, so they may occur at any point in your program. (Usually it\*(Aqs
83 best to keep them all together though.) They have their own namespace
84 apart from all the other "types" in Perl. This means that if you have a
85 function named "Foo", it is not the same thing as having a format named
86 "Foo". However, the default name for the format associated with a given
87 filehandle is the same as the name of the filehandle. Thus, the default
88 format for STDOUT is named "STDOUT", and the default format for filehandle
89 TEMP is named "TEMP". They just look the same. They aren\*(Aqt.
90 .PP
91 Output record formats are declared as follows:
92 .PP
93 .Vb 3
94 \& format NAME =
95 \& FORMLIST
96 \& .
97 .Ve
98 .PP
99 If the name is omitted, format "STDOUT" is defined. A single "." in
100 column 1 is used to terminate a format. FORMLIST consists of a sequence
101 of lines, each of which may be one of three types:
102 .IP 1. 4
103 A comment, indicated by putting a \*(Aq#\*(Aq in the first column.
104 .IP 2. 4
105 A "picture" line giving the format for one output line.
106 .IP 3. 4
107 An argument line supplying values to plug into the previous picture line.
108 .PP
109 Picture lines contain output field definitions, intermingled with
110 literal text. These lines do not undergo any kind of variable interpolation.
111 Field definitions are made up from a set of characters, for starting and
112 extending a field to its desired width. This is the complete set of
113 characters for field definitions:
114 .IX Xref "format, picture line @ ^ < | > # 0 . ... @* ^* ~ ~~"
115 .PP
116 .Vb 10
117 \& @ start of regular field
118 \& ^ start of special field
119 \& < pad character for left justification
120 \& | pad character for centering
121 \& > pad character for right justification
122 \& # pad character for a right\-justified numeric field
123 \& 0 instead of first #: pad number with leading zeroes
124 \& . decimal point within a numeric field
125 \& ... terminate a text field, show "..." as truncation evidence
126 \& @* variable width field for a multi\-line value
127 \& ^* variable width field for next line of a multi\-line value
128 \& ~ suppress line with all fields empty
129 \& ~~ repeat line until all fields are exhausted
130 .Ve
131 .PP
132 Each field in a picture line starts with either "@" (at) or "^" (caret),
133 indicating what we\*(Aqll call, respectively, a "regular" or "special" field.
134 The choice of pad characters determines whether a field is textual or
135 numeric. The tilde operators are not part of a field. Let\*(Aqs look at
136 the various possibilities in detail.
137 .SS "Text Fields"
138 .IX Xref "format, text field"
139 .IX Subsection "Text Fields"
140 The length of the field is supplied by padding out the field with multiple
141 "<", ">", or "|" characters to specify a non\-numeric field with,
142 respectively, left justification, right justification, or centering.
143 For a regular field, the value (up to the first newline) is taken and
144 printed according to the selected justification, truncating excess characters.
145 If you terminate a text field with "...", three dots will be shown if
146 the value is truncated. A special text field may be used to do rudimentary
147 multi\-line text block filling; see "Using Fill Mode" for details.
148 .PP
149 .Vb 7
150 \& Example:
151 \& format STDOUT =
152 \& @<<<<<< @|||||| @>>>>>>
153 \& "left", "middle", "right"
154 \& .
155 \& Output:
156 \& left middle right
157 .Ve
158 .SS "Numeric Fields"
159 .IX Xref "# format, numeric field"
160 .IX Subsection "Numeric Fields"
161 Using "#" as a padding character specifies a numeric field, with
162 right justification. An optional "." defines the position of the
163 decimal point. With a "0" (zero) instead of the first "#", the
164 formatted number will be padded with leading zeroes if necessary.
165 A special numeric field is blanked out if the value is undefined.
166 If the resulting value would exceed the width specified the field is
167 filled with "#" as overflow evidence.
168 .PP
169 .Vb 7
170 \& Example:
171 \& format STDOUT =
172 \& @### @.### @##.### @### @### ^####
173 \& 42, 3.1415, undef, 0, 10000, undef
174 \& .
175 \& Output:
176 \& 42 3.142 0.000 0 ####
177 .Ve
178 .SS "The Field @* for Variable\-Width Multi\-Line Text"
179 .IX Xref "@*"
180 .IX Subsection "The Field @* for Variable-Width Multi-Line Text"
181 The field "@*" can be used for printing multi\-line, nontruncated
182 values; it should (but need not) appear by itself on a line. A final
183 line feed is chomped off, but all other characters are emitted verbatim.
184 .SS "The Field ^* for Variable\-Width One\-line\-at\-a\-time Text"
185 .IX Xref "^*"
186 .IX Subsection "The Field ^* for Variable-Width One-line-at-a-time Text"
187 Like "@*", this is a variable\-width field. The value supplied must be a
188 scalar variable. Perl puts the first line (up to the first "\en") of the
189 text into the field, and then chops off the front of the string so that
190 the next time the variable is referenced, more of the text can be printed.
191 The variable will \fInot\fR be restored.
192 .PP
193 .Vb 12
194 \& Example:
195 \& $text = "line 1\enline 2\enline 3";
196 \& format STDOUT =
197 \& Text: ^*
198 \& $text
199 \& ~~ ^*
200 \& $text
201 \& .
202 \& Output:
203 \& Text: line 1
204 \& line 2
205 \& line 3
206 .Ve
207 .SS "Specifying Values"
208 .IX Xref "format, specifying values"
209 .IX Subsection "Specifying Values"
210 The values are specified on the following format line in the same order as
211 the picture fields. The expressions providing the values must be
212 separated by commas. They are all evaluated in a list context
213 before the line is processed, so a single list expression could produce
214 multiple list elements. The expressions may be spread out to more than
215 one line if enclosed in braces. If so, the opening brace must be the first
216 token on the first line. If an expression evaluates to a number with a
217 decimal part, and if the corresponding picture specifies that the decimal
218 part should appear in the output (that is, any picture except multiple "#"
219 characters \fBwithout\fR an embedded "."), the character used for the decimal
220 point is determined by the current LC_NUMERIC locale if \f(CW\*(C`use locale\*(C'\fR is in
221 effect. This means that, if, for example, the run\-time environment happens
222 to specify a German locale, "," will be used instead of the default ".". See
223 perllocale and "WARNINGS" for more information.
224 .SS "Using Fill Mode"
225 .IX Xref "format, fill mode"
226 .IX Subsection "Using Fill Mode"
227 On text fields the caret enables a kind of fill mode. Instead of an
228 arbitrary expression, the value supplied must be a scalar variable
229 that contains a text string. Perl puts the next portion of the text into
230 the field, and then chops off the front of the string so that the next time
231 the variable is referenced, more of the text can be printed. (Yes, this
232 means that the variable itself is altered during execution of the \fBwrite()\fR
233 call, and is not restored.) The next portion of text is determined by
234 a crude line\-breaking algorithm. You may use the carriage return character
235 (\f(CW\*(C`\er\*(C'\fR) to force a line break. You can change which characters are legal
236 to break on by changing the variable \f(CW$:\fR (that\*(Aqs
237 \&\f(CW$FORMAT_LINE_BREAK_CHARACTERS\fR if you\*(Aqre using the English module) to a
238 list of the desired characters.
239 .PP
240 Normally you would use a sequence of fields in a vertical stack associated
241 with the same scalar variable to print out a block of text. You might wish
242 to end the final field with the text "...", which will appear in the output
243 if the text was too long to appear in its entirety.
244 .SS "Suppressing Lines Where All Fields Are Void"
245 .IX Xref "format, suppressing lines"
246 .IX Subsection "Suppressing Lines Where All Fields Are Void"
247 Using caret fields can produce lines where all fields are blank. You can
248 suppress such lines by putting a "~" (tilde) character anywhere in the
249 line. The tilde will be translated to a space upon output.
250 .SS "Repeating Format Lines"
251 .IX Xref "format, repeating lines"
252 .IX Subsection "Repeating Format Lines"
253 If you put two contiguous tilde characters "~~" anywhere into a line,
254 then in addition to suppressing the line if all fields are blank,
255 the line will be repeated until all the fields on the line are exhausted,
256 i.e. undefined. For special (caret) text fields this will occur sooner or
257 later, but if you use a text field of the at variety, the expression you
258 supply had better not give the same value every time forever! (\f(CWshift(@f)\fR
259 is a simple example that would work.) Don\*(Aqt use a regular (at) numeric
260 field in such lines, because it will never go blank.
261 .SS "Top of Form Processing"
262 .IX Xref "format, top of form top header"
263 .IX Subsection "Top of Form Processing"
264 Top\-of\-form processing is by default handled by a format with the
265 same name as the current filehandle with "_TOP" concatenated to it.
266 It\*(Aqs triggered at the top of each page. See "write" in perlfunc.
267 .PP
268 Examples:
269 .PP
270 .Vb 10
271 \& # a report on the /etc/passwd file
272 \& format STDOUT_TOP =
273 \& Passwd File
274 \& Name Login Office Uid Gid Home
275 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
276 \& .
277 \& format STDOUT =
278 \& @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
279 \& $name, $login, $office,$uid,$gid, $home
280 \& .
281 \&
282 \&
283 \& # a report from a bug report form
284 \& format STDOUT_TOP =
285 \& Bug Reports
286 \& @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>
287 \& $system, $%, $date
288 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
289 \& .
290 \& format STDOUT =
291 \& Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
292 \& $subject
293 \& Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
294 \& $index, $description
295 \& Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
296 \& $priority, $date, $description
297 \& From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
298 \& $from, $description
299 \& Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
300 \& $programmer, $description
301 \& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
302 \& $description
303 \& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
304 \& $description
305 \& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
306 \& $description
307 \& ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
308 \& $description
309 \& ~ ^<<<<<<<<<<<<<<<<<<<<<<<...
310 \& $description
311 \& .
312 .Ve
313 .PP
314 It is possible to intermix \fBprint()\fRs with \fBwrite()\fRs on the same output
315 channel, but you\*(Aqll have to handle \f(CW\*(C`$\-\*(C'\fR (\f(CW$FORMAT_LINES_LEFT\fR)
316 yourself.
317 .SS "Format Variables"
318 .IX Xref "format variables format, variables"
319 .IX Subsection "Format Variables"
320 The current format name is stored in the variable \f(CW$~\fR (\f(CW$FORMAT_NAME\fR),
321 and the current top of form format name is in \f(CW$^\fR (\f(CW$FORMAT_TOP_NAME\fR).
322 The current output page number is stored in \f(CW$%\fR (\f(CW$FORMAT_PAGE_NUMBER\fR),
323 and the number of lines on the page is in \f(CW$=\fR (\f(CW$FORMAT_LINES_PER_PAGE\fR).
324 Whether to autoflush output on this handle is stored in \f(CW$|\fR
325 (\f(CW$OUTPUT_AUTOFLUSH\fR). The string output before each top of page (except
326 the first) is stored in \f(CW$^L\fR (\f(CW$FORMAT_FORMFEED\fR). These variables are
327 set on a per\-filehandle basis, so you\*(Aqll need to \fBselect()\fR into a different
328 one to affect them:
329 .PP
330 .Vb 4
331 \& select((select(OUTF),
332 \& $~ = "My_Other_Format",
333 \& $^ = "My_Top_Format"
334 \& )[0]);
335 .Ve
336 .PP
337 Pretty ugly, eh? It\*(Aqs a common idiom though, so don\*(Aqt be too surprised
338 when you see it. You can at least use a temporary variable to hold
339 the previous filehandle: (this is a much better approach in general,
340 because not only does legibility improve, you now have an intermediary
341 stage in the expression to single\-step the debugger through):
342 .PP
343 .Vb 4
344 \& $ofh = select(OUTF);
345 \& $~ = "My_Other_Format";
346 \& $^ = "My_Top_Format";
347 \& select($ofh);
348 .Ve
349 .PP
350 If you use the English module, you can even read the variable names:
351 .PP
352 .Vb 5
353 \& use English;
354 \& $ofh = select(OUTF);
355 \& $FORMAT_NAME = "My_Other_Format";
356 \& $FORMAT_TOP_NAME = "My_Top_Format";
357 \& select($ofh);
358 .Ve
359 .PP
360 But you still have those funny \fBselect()\fRs. So just use the FileHandle
361 module. Now, you can access these special variables using lowercase
362 method names instead:
363 .PP
364 .Vb 3
365 \& use FileHandle;
366 \& format_name OUTF "My_Other_Format";
367 \& format_top_name OUTF "My_Top_Format";
368 .Ve
369 .PP
370 Much better!
371 .SH NOTES
372 .IX Header "NOTES"
373 Because the values line may contain arbitrary expressions (for at fields,
374 not caret fields), you can farm out more sophisticated processing
375 to other functions, like \fBsprintf()\fR or one of your own. For example:
376 .PP
377 .Vb 4
378 \& format Ident =
379 \& @<<<<<<<<<<<<<<<
380 \& &commify($n)
381 \& .
382 .Ve
383 .PP
384 To get a real at or caret into the field, do this:
385 .PP
386 .Vb 4
387 \& format Ident =
388 \& I have an @ here.
389 \& "@"
390 \& .
391 .Ve
392 .PP
393 To center a whole line of text, do something like this:
394 .PP
395 .Vb 4
396 \& format Ident =
397 \& @|||||||||||||||||||||||||||||||||||||||||||||||
398 \& "Some text line"
399 \& .
400 .Ve
401 .PP
402 There is no builtin way to say "float this to the right hand side
403 of the page, however wide it is." You have to specify where it goes.
404 The truly desperate can generate their own format on the fly, based
405 on the current number of columns, and then \fBeval()\fR it:
406 .PP
407 .Vb 9
408 \& $format = "format STDOUT = \en"
409 \& . \*(Aq^\*(Aq . \*(Aq<\*(Aq x $cols . "\en"
410 \& . \*(Aq$entry\*(Aq . "\en"
411 \& . "\et^" . "<" x ($cols\-8) . "~~\en"
412 \& . \*(Aq$entry\*(Aq . "\en"
413 \& . ".\en";
414 \& print $format if $Debugging;
415 \& eval $format;
416 \& die $@ if $@;
417 .Ve
418 .PP
419 Which would generate a format looking something like this:
420 .PP
421 .Vb 6
422 \& format STDOUT =
423 \& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
424 \& $entry
425 \& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
426 \& $entry
427 \& .
428 .Ve
429 .PP
430 Here\*(Aqs a little program that\*(Aqs somewhat like \fBfmt\fR\|(1):
431 .PP
432 .Vb 3
433 \& format =
434 \& ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
435 \& $_
436 \&
437 \& .
438 \&
439 \& $/ = \*(Aq\*(Aq;
440 \& while (<>) {
441 \& s/\es*\en\es*/ /g;
442 \& write;
443 \& }
444 .Ve
445 .SS Footers
446 .IX Xref "format, footer footer"
447 .IX Subsection "Footers"
448 While \f(CW$FORMAT_TOP_NAME\fR contains the name of the current header format,
449 there is no corresponding mechanism to automatically do the same thing
450 for a footer. Not knowing how big a format is going to be until you
451 evaluate it is one of the major problems. It\*(Aqs on the TODO list.
452 .PP
453 Here\*(Aqs one strategy: If you have a fixed\-size footer, you can get footers
454 by checking \f(CW$FORMAT_LINES_LEFT\fR before each \fBwrite()\fR and print the footer
455 yourself if necessary.
456 .PP
457 Here\*(Aqs another strategy: Open a pipe to yourself, using \f(CW\*(C`open(MYSELF, "|\-")\*(C'\fR
458 (see "open" in perlfunc) and always \fBwrite()\fR to MYSELF instead of STDOUT.
459 Have your child process massage its STDIN to rearrange headers and footers
460 however you like. Not very convenient, but doable.
461 .SS "Accessing Formatting Internals"
462 .IX Xref "format, internals"
463 .IX Subsection "Accessing Formatting Internals"
464 For low\-level access to the formatting mechanism, you may use \fBformline()\fR
465 and access \f(CW$^A\fR (the \f(CW$ACCUMULATOR\fR variable) directly.
466 .PP
467 For example:
468 .PP
469 .Vb 3
470 \& $str = formline <<\*(AqEND\*(Aq, 1,2,3;
471 \& @<<< @||| @>>>
472 \& END
473 \&
474 \& print "Wow, I just stored \*(Aq$^A\*(Aq in the accumulator!\en";
475 .Ve
476 .PP
477 Or to make an \fBswrite()\fR subroutine, which is to \fBwrite()\fR what \fBsprintf()\fR
478 is to \fBprintf()\fR, do this:
479 .PP
480 .Vb 8
481 \& use Carp;
482 \& sub swrite {
483 \& croak "usage: swrite PICTURE ARGS" unless @_;
484 \& my $format = shift;
485 \& $^A = "";
486 \& formline($format,@_);
487 \& return $^A;
488 \& }
489 \&
490 \& $string = swrite(<<\*(AqEND\*(Aq, 1, 2, 3);
491 \& Check me out
492 \& @<<< @||| @>>>
493 \& END
494 \& print $string;
495 .Ve
496 .SH WARNINGS
497 .IX Header "WARNINGS"
498 The lone dot that ends a format can also prematurely end a mail
499 message passing through a misconfigured Internet mailer (and based on
500 experience, such misconfiguration is the rule, not the exception). So
501 when sending format code through mail, you should indent it so that
502 the format\-ending dot is not on the left margin; this will prevent
503 SMTP cutoff.
504 .PP
505 Lexical variables (declared with "my") are not visible within a
506 format unless the format is declared within the scope of the lexical
507 variable.
508 .PP
509 If a program\*(Aqs environment specifies an LC_NUMERIC locale and \f(CW\*(C`use
510 locale\*(C'\fR is in effect when the format is declared, the locale is used
511 to specify the decimal point character in formatted output. Formatted
512 output cannot be controlled by \f(CW\*(C`use locale\*(C'\fR at the time when \fBwrite()\fR
513 is called. See perllocale for further discussion of locale handling.
514 .PP
515 Within strings that are to be displayed in a fixed\-length text field,
516 each control character is substituted by a space. (But remember the
517 special meaning of \f(CW\*(C`\er\*(C'\fR when using fill mode.) This is done to avoid
518 misalignment when control characters "disappear" on some output media.