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