Commit | Line | Data |
a0d0e21e |
1 | =head1 NAME |
2 | |
3 | perlform - Perl formats |
4 | |
5 | =head1 DESCRIPTION |
6 | |
7 | Perl has a mechanism to help you generate simple reports and charts. To |
54310121 |
8 | facilitate this, Perl helps you code up your output page close to how it |
9 | will look when it's printed. It can keep track of things like how many |
10 | lines are on a page, what page you're on, when to print page headers, |
11 | etc. Keywords are borrowed from FORTRAN: format() to declare and write() |
12 | to execute; see their entries in L<perlfunc>. Fortunately, the layout is |
13 | much more legible, more like BASIC's PRINT USING statement. Think of it |
14 | as a poor man's nroff(1). |
15 | |
16 | Formats, like packages and subroutines, are declared rather than |
17 | executed, so they may occur at any point in your program. (Usually it's |
18 | best to keep them all together though.) They have their own namespace |
19 | apart from all the other "types" in Perl. This means that if you have a |
20 | function named "Foo", it is not the same thing as having a format named |
21 | "Foo". However, the default name for the format associated with a given |
a0d0e21e |
22 | filehandle is the same as the name of the filehandle. Thus, the default |
7b8d334a |
23 | format for STDOUT is named "STDOUT", and the default format for filehandle |
24 | TEMP is named "TEMP". They just look the same. They aren't. |
a0d0e21e |
25 | |
26 | Output record formats are declared as follows: |
27 | |
28 | format NAME = |
29 | FORMLIST |
30 | . |
31 | |
6aa7c346 |
32 | If name is omitted, format "STDOUT" is defined. FORMLIST consists of |
54310121 |
33 | a sequence of lines, each of which may be one of three types: |
a0d0e21e |
34 | |
35 | =over 4 |
36 | |
37 | =item 1. |
38 | |
39 | A comment, indicated by putting a '#' in the first column. |
40 | |
41 | =item 2. |
42 | |
43 | A "picture" line giving the format for one output line. |
44 | |
45 | =item 3. |
46 | |
47 | An argument line supplying values to plug into the previous picture line. |
48 | |
49 | =back |
50 | |
51 | Picture lines are printed exactly as they look, except for certain fields |
52 | that substitute values into the line. Each field in a picture line starts |
53 | with either "@" (at) or "^" (caret). These lines do not undergo any kind |
54 | of variable interpolation. The at field (not to be confused with the array |
55 | marker @) is the normal kind of field; the other kind, caret fields, are used |
4a6725af |
56 | to do rudimentary multi-line text block filling. The length of the field |
1fef88e7 |
57 | is supplied by padding out the field with multiple "E<lt>", "E<gt>", or "|" |
a0d0e21e |
58 | characters to specify, respectively, left justification, right |
59 | justification, or centering. If the variable would exceed the width |
60 | specified, it is truncated. |
61 | |
62 | As an alternate form of right justification, you may also use "#" |
0f1f4321 |
63 | characters (with an optional ".") to specify a numeric field. This way |
64 | you can line up the decimal points. With a "0" (zero) instead of the |
65 | first "#", the formatted number will be padded with leading zeroes if |
66 | necessary. If any value supplied for these fields contains a newline, |
67 | only the text up to the newline is printed. Finally, the special field |
68 | "@*" can be used for printing multi-line, nontruncated values; it |
69 | should appear by itself on a line. |
a0d0e21e |
70 | |
71 | The values are specified on the following line in the same order as |
72 | the picture fields. The expressions providing the values should be |
73 | separated by commas. The expressions are all evaluated in a list context |
74 | before the line is processed, so a single list expression could produce |
75 | multiple list elements. The expressions may be spread out to more than |
76 | one line if enclosed in braces. If so, the opening brace must be the first |
a034a98d |
77 | token on the first line. If an expression evaluates to a number with a |
78 | decimal part, and if the corresponding picture specifies that the decimal |
79 | part should appear in the output (that is, any picture except multiple "#" |
80 | characters B<without> an embedded "."), the character used for the decimal |
81 | point is B<always> determined by the current LC_NUMERIC locale. This |
82 | means that, if, for example, the run-time environment happens to specify a |
83 | German locale, "," will be used instead of the default ".". See |
84 | L<perllocale> and L<"WARNINGS"> for more information. |
a0d0e21e |
85 | |
86 | Picture fields that begin with ^ rather than @ are treated specially. |
87 | With a # field, the field is blanked out if the value is undefined. For |
88 | other field types, the caret enables a kind of fill mode. Instead of an |
89 | arbitrary expression, the value supplied must be a scalar variable name |
90 | that contains a text string. Perl puts as much text as it can into the |
91 | field, and then chops off the front of the string so that the next time |
92 | the variable is referenced, more of the text can be printed. (Yes, this |
93 | means that the variable itself is altered during execution of the write() |
94 | call, and is not returned.) Normally you would use a sequence of fields |
95 | in a vertical stack to print out a block of text. You might wish to end |
96 | the final field with the text "...", which will appear in the output if |
97 | the text was too long to appear in its entirety. You can change which |
98 | characters are legal to break on by changing the variable C<$:> (that's |
99 | $FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a |
100 | list of the desired characters. |
101 | |
748a9306 |
102 | Using caret fields can produce variable length records. If the text |
a0d0e21e |
103 | to be formatted is short, you can suppress blank lines by putting a |
104 | "~" (tilde) character anywhere in the line. The tilde will be translated |
105 | to a space upon output. If you put a second tilde contiguous to the |
106 | first, the line will be repeated until all the fields on the line are |
107 | exhausted. (If you use a field of the at variety, the expression you |
108 | supply had better not give the same value every time forever!) |
109 | |
54310121 |
110 | Top-of-form processing is by default handled by a format with the |
a0d0e21e |
111 | same name as the current filehandle with "_TOP" concatenated to it. |
a2eb9003 |
112 | It's triggered at the top of each page. See L<perlfunc/write>. |
a0d0e21e |
113 | |
114 | Examples: |
115 | |
116 | # a report on the /etc/passwd file |
117 | format STDOUT_TOP = |
118 | Passwd File |
119 | Name Login Office Uid Gid Home |
120 | ------------------------------------------------------------------ |
121 | . |
122 | format STDOUT = |
123 | @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<< |
124 | $name, $login, $office,$uid,$gid, $home |
125 | . |
126 | |
127 | |
128 | # a report from a bug report form |
129 | format STDOUT_TOP = |
130 | Bug Reports |
131 | @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> |
132 | $system, $%, $date |
133 | ------------------------------------------------------------------ |
134 | . |
135 | format STDOUT = |
136 | Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
137 | $subject |
138 | Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
139 | $index, $description |
140 | Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
141 | $priority, $date, $description |
142 | From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
143 | $from, $description |
144 | Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
145 | $programmer, $description |
146 | ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
147 | $description |
148 | ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
149 | $description |
150 | ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
151 | $description |
152 | ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
153 | $description |
154 | ~ ^<<<<<<<<<<<<<<<<<<<<<<<... |
155 | $description |
156 | . |
157 | |
158 | It is possible to intermix print()s with write()s on the same output |
1fef88e7 |
159 | channel, but you'll have to handle C<$-> (C<$FORMAT_LINES_LEFT>) |
a0d0e21e |
160 | yourself. |
161 | |
162 | =head2 Format Variables |
163 | |
1fef88e7 |
164 | The current format name is stored in the variable C<$~> (C<$FORMAT_NAME>), |
165 | and the current top of form format name is in C<$^> (C<$FORMAT_TOP_NAME>). |
166 | The current output page number is stored in C<$%> (C<$FORMAT_PAGE_NUMBER>), |
167 | and the number of lines on the page is in C<$=> (C<$FORMAT_LINES_PER_PAGE>). |
748a9306 |
168 | Whether to autoflush output on this handle is stored in C<$|> |
1fef88e7 |
169 | (C<$OUTPUT_AUTOFLUSH>). The string output before each top of page (except |
170 | the first) is stored in C<$^L> (C<$FORMAT_FORMFEED>). These variables are |
a0d0e21e |
171 | set on a per-filehandle basis, so you'll need to select() into a different |
172 | one to affect them: |
173 | |
54310121 |
174 | select((select(OUTF), |
a0d0e21e |
175 | $~ = "My_Other_Format", |
176 | $^ = "My_Top_Format" |
177 | )[0]); |
178 | |
179 | Pretty ugly, eh? It's a common idiom though, so don't be too surprised |
180 | when you see it. You can at least use a temporary variable to hold |
181 | the previous filehandle: (this is a much better approach in general, |
182 | because not only does legibility improve, you now have intermediary |
183 | stage in the expression to single-step the debugger through): |
184 | |
185 | $ofh = select(OUTF); |
186 | $~ = "My_Other_Format"; |
187 | $^ = "My_Top_Format"; |
188 | select($ofh); |
189 | |
190 | If you use the English module, you can even read the variable names: |
191 | |
a1ce9542 |
192 | use English '-no_match_vars'; |
a0d0e21e |
193 | $ofh = select(OUTF); |
194 | $FORMAT_NAME = "My_Other_Format"; |
195 | $FORMAT_TOP_NAME = "My_Top_Format"; |
196 | select($ofh); |
197 | |
198 | But you still have those funny select()s. So just use the FileHandle |
68dc0745 |
199 | module. Now, you can access these special variables using lowercase |
a0d0e21e |
200 | method names instead: |
201 | |
202 | use FileHandle; |
203 | format_name OUTF "My_Other_Format"; |
204 | format_top_name OUTF "My_Top_Format"; |
205 | |
206 | Much better! |
207 | |
208 | =head1 NOTES |
209 | |
54310121 |
210 | Because the values line may contain arbitrary expressions (for at fields, |
748a9306 |
211 | not caret fields), you can farm out more sophisticated processing |
a0d0e21e |
212 | to other functions, like sprintf() or one of your own. For example: |
213 | |
54310121 |
214 | format Ident = |
a0d0e21e |
215 | @<<<<<<<<<<<<<<< |
216 | &commify($n) |
217 | . |
218 | |
219 | To get a real at or caret into the field, do this: |
220 | |
54310121 |
221 | format Ident = |
a0d0e21e |
222 | I have an @ here. |
223 | "@" |
224 | . |
225 | |
226 | To center a whole line of text, do something like this: |
227 | |
54310121 |
228 | format Ident = |
a0d0e21e |
229 | @||||||||||||||||||||||||||||||||||||||||||||||| |
230 | "Some text line" |
231 | . |
232 | |
233 | There is no builtin way to say "float this to the right hand side |
234 | of the page, however wide it is." You have to specify where it goes. |
235 | The truly desperate can generate their own format on the fly, based |
236 | on the current number of columns, and then eval() it: |
237 | |
5a964f20 |
238 | $format = "format STDOUT = \n" |
239 | . '^' . '<' x $cols . "\n" |
240 | . '$entry' . "\n" |
241 | . "\t^" . "<" x ($cols-8) . "~~\n" |
242 | . '$entry' . "\n" |
a0d0e21e |
243 | . ".\n"; |
244 | print $format if $Debugging; |
54310121 |
245 | eval $format; |
a0d0e21e |
246 | die $@ if $@; |
247 | |
248 | Which would generate a format looking something like this: |
249 | |
54310121 |
250 | format STDOUT = |
a0d0e21e |
251 | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
252 | $entry |
253 | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ |
254 | $entry |
255 | . |
256 | |
257 | Here's a little program that's somewhat like fmt(1): |
258 | |
54310121 |
259 | format = |
a0d0e21e |
260 | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ |
261 | $_ |
262 | |
263 | . |
264 | |
265 | $/ = ''; |
266 | while (<>) { |
267 | s/\s*\n\s*/ /g; |
268 | write; |
54310121 |
269 | } |
a0d0e21e |
270 | |
271 | =head2 Footers |
272 | |
273 | While $FORMAT_TOP_NAME contains the name of the current header format, |
274 | there is no corresponding mechanism to automatically do the same thing |
275 | for a footer. Not knowing how big a format is going to be until you |
276 | evaluate it is one of the major problems. It's on the TODO list. |
277 | |
278 | Here's one strategy: If you have a fixed-size footer, you can get footers |
279 | by checking $FORMAT_LINES_LEFT before each write() and print the footer |
280 | yourself if necessary. |
281 | |
54310121 |
282 | Here's another strategy: Open a pipe to yourself, using C<open(MYSELF, "|-")> |
283 | (see L<perlfunc/open()>) and always write() to MYSELF instead of STDOUT. |
284 | Have your child process massage its STDIN to rearrange headers and footers |
285 | however you like. Not very convenient, but doable. |
a0d0e21e |
286 | |
287 | =head2 Accessing Formatting Internals |
288 | |
289 | For low-level access to the formatting mechanism. you may use formline() |
290 | and access C<$^A> (the $ACCUMULATOR variable) directly. |
291 | |
292 | For example: |
293 | |
294 | $str = formline <<'END', 1,2,3; |
295 | @<<< @||| @>>> |
296 | END |
297 | |
298 | print "Wow, I just stored `$^A' in the accumulator!\n"; |
299 | |
5a964f20 |
300 | Or to make an swrite() subroutine, which is to write() what sprintf() |
a0d0e21e |
301 | is to printf(), do this: |
302 | |
a0d0e21e |
303 | use Carp; |
304 | sub swrite { |
748a9306 |
305 | croak "usage: swrite PICTURE ARGS" unless @_; |
306 | my $format = shift; |
307 | $^A = ""; |
308 | formline($format,@_); |
309 | return $^A; |
54310121 |
310 | } |
a0d0e21e |
311 | |
312 | $string = swrite(<<'END', 1, 2, 3); |
313 | Check me out |
314 | @<<< @||| @>>> |
315 | END |
316 | print $string; |
317 | |
a034a98d |
318 | =head1 WARNINGS |
a0d0e21e |
319 | |
5a964f20 |
320 | The lone dot that ends a format can also prematurely end a mail |
6aa7c346 |
321 | message passing through a misconfigured Internet mailer (and based on |
322 | experience, such misconfiguration is the rule, not the exception). So |
5a964f20 |
323 | when sending format code through mail, you should indent it so that |
6aa7c346 |
324 | the format-ending dot is not on the left margin; this will prevent |
5a964f20 |
325 | SMTP cutoff. |
6aa7c346 |
326 | |
748a9306 |
327 | Lexical variables (declared with "my") are not visible within a |
328 | format unless the format is declared within the scope of the lexical |
6aa7c346 |
329 | variable. (They weren't visible at all before version 5.001.) |
a034a98d |
330 | |
5a964f20 |
331 | Formats are the only part of Perl that unconditionally use information |
a034a98d |
332 | from a program's locale; if a program's environment specifies an |
333 | LC_NUMERIC locale, it is always used to specify the decimal point |
334 | character in formatted output. Perl ignores all other aspects of locale |
335 | handling unless the C<use locale> pragma is in effect. Formatted output |
336 | cannot be controlled by C<use locale> because the pragma is tied to the |
337 | block structure of the program, and, for historical reasons, formats |
338 | exist outside that block structure. See L<perllocale> for further |
339 | discussion of locale handling. |
c380484f |
340 | |
341 | Inside of an expression, the whitespace characters \n, \t and \f are |
342 | considered to be equivalent to a single space. Thus, you could think |
343 | of this filter being applied to each value in the format: |
344 | |
345 | $value =~ tr/\n\t\f/ /; |
346 | |
347 | The remaining whitespace character, \r, forces the printing of a new |
348 | line if allowed by the picture line. |