Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
14 KiB

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