1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlform - Perl formats 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gatePerl has a mechanism to help you generate simple reports and charts. To 8*0Sstevel@tonic-gatefacilitate this, Perl helps you code up your output page close to how it 9*0Sstevel@tonic-gatewill look when it's printed. It can keep track of things like how many 10*0Sstevel@tonic-gatelines are on a page, what page you're on, when to print page headers, 11*0Sstevel@tonic-gateetc. Keywords are borrowed from FORTRAN: format() to declare and write() 12*0Sstevel@tonic-gateto execute; see their entries in L<perlfunc>. Fortunately, the layout is 13*0Sstevel@tonic-gatemuch more legible, more like BASIC's PRINT USING statement. Think of it 14*0Sstevel@tonic-gateas a poor man's nroff(1). 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gateFormats, like packages and subroutines, are declared rather than 17*0Sstevel@tonic-gateexecuted, so they may occur at any point in your program. (Usually it's 18*0Sstevel@tonic-gatebest to keep them all together though.) They have their own namespace 19*0Sstevel@tonic-gateapart from all the other "types" in Perl. This means that if you have a 20*0Sstevel@tonic-gatefunction named "Foo", it is not the same thing as having a format named 21*0Sstevel@tonic-gate"Foo". However, the default name for the format associated with a given 22*0Sstevel@tonic-gatefilehandle is the same as the name of the filehandle. Thus, the default 23*0Sstevel@tonic-gateformat for STDOUT is named "STDOUT", and the default format for filehandle 24*0Sstevel@tonic-gateTEMP is named "TEMP". They just look the same. They aren't. 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gateOutput record formats are declared as follows: 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate format NAME = 29*0Sstevel@tonic-gate FORMLIST 30*0Sstevel@tonic-gate . 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gateIf the name is omitted, format "STDOUT" is defined. A single "." in 33*0Sstevel@tonic-gatecolumn 1 is used to terminate a format. FORMLIST consists of a sequence 34*0Sstevel@tonic-gateof lines, each of which may be one of three types: 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate=over 4 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate=item 1. 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gateA comment, indicated by putting a '#' in the first column. 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate=item 2. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gateA "picture" line giving the format for one output line. 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate=item 3. 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gateAn argument line supplying values to plug into the previous picture line. 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate=back 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gatePicture lines contain output field definitions, intermingled with 53*0Sstevel@tonic-gateliteral text. These lines do not undergo any kind of variable interpolation. 54*0Sstevel@tonic-gateField definitions are made up from a set of characters, for starting and 55*0Sstevel@tonic-gateextending a field to its desired width. This is the complete set of 56*0Sstevel@tonic-gatecharacters for field definitions: 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate @ start of regular field 59*0Sstevel@tonic-gate ^ start of special field 60*0Sstevel@tonic-gate < pad character for left adjustification 61*0Sstevel@tonic-gate | pad character for centering 62*0Sstevel@tonic-gate > pad character for right adjustificat 63*0Sstevel@tonic-gate # pad character for a right justified numeric field 64*0Sstevel@tonic-gate 0 instead of first #: pad number with leading zeroes 65*0Sstevel@tonic-gate . decimal point within a numeric field 66*0Sstevel@tonic-gate ... terminate a text field, show "..." as truncation evidence 67*0Sstevel@tonic-gate @* variable width field for a multi-line value 68*0Sstevel@tonic-gate ^* variable width field for next line of a multi-line value 69*0Sstevel@tonic-gate ~ suppress line with all fields empty 70*0Sstevel@tonic-gate ~~ repeat line until all fields are exhausted 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gateEach field in a picture line starts with either "@" (at) or "^" (caret), 73*0Sstevel@tonic-gateindicating what we'll call, respectively, a "regular" or "special" field. 74*0Sstevel@tonic-gateThe choice of pad characters determines whether a field is textual or 75*0Sstevel@tonic-gatenumeric. The tilde operators are not part of a field. Let's look at 76*0Sstevel@tonic-gatethe various possibilities in detail. 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate=head2 Text Fields 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gateThe length of the field is supplied by padding out the field with multiple 82*0Sstevel@tonic-gate"E<lt>", "E<gt>", or "|" characters to specify a non-numeric field with, 83*0Sstevel@tonic-gaterespectively, left justification, right justification, or centering. 84*0Sstevel@tonic-gateFor a regular field, the value (up to the first newline) is taken and 85*0Sstevel@tonic-gateprinted according to the selected justification, truncating excess characters. 86*0Sstevel@tonic-gateIf you terminate a text field with "...", three dots will be shown if 87*0Sstevel@tonic-gatethe value is truncated. A special text field may be used to do rudimentary 88*0Sstevel@tonic-gatemulti-line text block filling; see L</Using Fill Mode> for details. 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate Example: 91*0Sstevel@tonic-gate format STDOUT = 92*0Sstevel@tonic-gate @<<<<<< @|||||| @>>>>>> 93*0Sstevel@tonic-gate "left", "middle", "right" 94*0Sstevel@tonic-gate . 95*0Sstevel@tonic-gate Output: 96*0Sstevel@tonic-gate left middle right 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate=head2 Numeric Fields 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gateUsing "#" as a padding character specifies a numeric field, with 102*0Sstevel@tonic-gateright justification. An optional "." defines the position of the 103*0Sstevel@tonic-gatedecimal point. With a "0" (zero) instead of the first "#", the 104*0Sstevel@tonic-gateformatted number will be padded with leading zeroes if necessary. 105*0Sstevel@tonic-gateA special numeric field is blanked out if the value is undefined. 106*0Sstevel@tonic-gateIf the resulting value would exceed the width specified the field is 107*0Sstevel@tonic-gatefilled with "#" as overflow evidence. 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate Example: 110*0Sstevel@tonic-gate format STDOUT = 111*0Sstevel@tonic-gate @### @.### @##.### @### @### ^#### 112*0Sstevel@tonic-gate 42, 3.1415, undef, 0, 10000, undef 113*0Sstevel@tonic-gate . 114*0Sstevel@tonic-gate Output: 115*0Sstevel@tonic-gate 42 3.142 0.000 0 #### 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate=head2 The Field @* for Variable Width Multi-Line Text 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateThe field "@*" can be used for printing multi-line, nontruncated 121*0Sstevel@tonic-gatevalues; it should (but need not) appear by itself on a line. A final 122*0Sstevel@tonic-gateline feed is chomped off, but all other characters are emitted verbatim. 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate=head2 The Field ^* for Variable Width One-line-at-a-time Text 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gateLike "@*", this is a variable width field. The value supplied must be a 128*0Sstevel@tonic-gatescalar variable. Perl puts the first line (up to the first "\n") of the 129*0Sstevel@tonic-gatetext into the field, and then chops off the front of the string so that 130*0Sstevel@tonic-gatethe next time the variable is referenced, more of the text can be printed. 131*0Sstevel@tonic-gateThe variable will I<not> be restored. 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate Example: 134*0Sstevel@tonic-gate $text = "line 1\nline 2\nline 3"; 135*0Sstevel@tonic-gate format STDOUT = 136*0Sstevel@tonic-gate Text: ^* 137*0Sstevel@tonic-gate $text 138*0Sstevel@tonic-gate ~~ ^* 139*0Sstevel@tonic-gate $text 140*0Sstevel@tonic-gate . 141*0Sstevel@tonic-gate Output: 142*0Sstevel@tonic-gate Text: line 1 143*0Sstevel@tonic-gate line 2 144*0Sstevel@tonic-gate line 3 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate=head2 Specifying Values 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gateThe values are specified on the following format line in the same order as 150*0Sstevel@tonic-gatethe picture fields. The expressions providing the values must be 151*0Sstevel@tonic-gateseparated by commas. They are all evaluated in a list context 152*0Sstevel@tonic-gatebefore the line is processed, so a single list expression could produce 153*0Sstevel@tonic-gatemultiple list elements. The expressions may be spread out to more than 154*0Sstevel@tonic-gateone line if enclosed in braces. If so, the opening brace must be the first 155*0Sstevel@tonic-gatetoken on the first line. If an expression evaluates to a number with a 156*0Sstevel@tonic-gatedecimal part, and if the corresponding picture specifies that the decimal 157*0Sstevel@tonic-gatepart should appear in the output (that is, any picture except multiple "#" 158*0Sstevel@tonic-gatecharacters B<without> an embedded "."), the character used for the decimal 159*0Sstevel@tonic-gatepoint is B<always> determined by the current LC_NUMERIC locale. This 160*0Sstevel@tonic-gatemeans that, if, for example, the run-time environment happens to specify a 161*0Sstevel@tonic-gateGerman locale, "," will be used instead of the default ".". See 162*0Sstevel@tonic-gateL<perllocale> and L<"WARNINGS"> for more information. 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate=head2 Using Fill Mode 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gateOn text fields the caret enables a kind of fill mode. Instead of an 168*0Sstevel@tonic-gatearbitrary expression, the value supplied must be a scalar variable 169*0Sstevel@tonic-gatethat contains a text string. Perl puts the next portion of the text into 170*0Sstevel@tonic-gatethe field, and then chops off the front of the string so that the next time 171*0Sstevel@tonic-gatethe variable is referenced, more of the text can be printed. (Yes, this 172*0Sstevel@tonic-gatemeans that the variable itself is altered during execution of the write() 173*0Sstevel@tonic-gatecall, and is not restored.) The next portion of text is determined by 174*0Sstevel@tonic-gatea crude line breaking algorithm. You may use the carriage return character 175*0Sstevel@tonic-gate(C<\r>) to force a line break. You can change which characters are legal 176*0Sstevel@tonic-gateto break on by changing the variable C<$:> (that's 177*0Sstevel@tonic-gate$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a 178*0Sstevel@tonic-gatelist of the desired characters. 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gateNormally you would use a sequence of fields in a vertical stack associated 181*0Sstevel@tonic-gatewith the same scalar variable to print out a block of text. You might wish 182*0Sstevel@tonic-gateto end the final field with the text "...", which will appear in the output 183*0Sstevel@tonic-gateif the text was too long to appear in its entirety. 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate=head2 Suppressing Lines Where All Fields Are Void 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gateUsing caret fields can produce lines where all fields are blank. You can 189*0Sstevel@tonic-gatesuppress such lines by putting a "~" (tilde) character anywhere in the 190*0Sstevel@tonic-gateline. The tilde will be translated to a space upon output. 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate=head2 Repeating Format Lines 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gateIf you put two contiguous tilde characters "~~" anywhere into a line, 196*0Sstevel@tonic-gatethe line will be repeated until all the fields on the line are exhausted, 197*0Sstevel@tonic-gatei.e. undefined. For special (caret) text fields this will occur sooner or 198*0Sstevel@tonic-gatelater, but if you use a text field of the at variety, the expression you 199*0Sstevel@tonic-gatesupply had better not give the same value every time forever! (C<shift(@f)> 200*0Sstevel@tonic-gateis a simple example that would work.) Don't use a regular (at) numeric 201*0Sstevel@tonic-gatefield in such lines, because it will never go blank. 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate=head2 Top of Form Processing 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gateTop-of-form processing is by default handled by a format with the 207*0Sstevel@tonic-gatesame name as the current filehandle with "_TOP" concatenated to it. 208*0Sstevel@tonic-gateIt's triggered at the top of each page. See L<perlfunc/write>. 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gateExamples: 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate # a report on the /etc/passwd file 213*0Sstevel@tonic-gate format STDOUT_TOP = 214*0Sstevel@tonic-gate Passwd File 215*0Sstevel@tonic-gate Name Login Office Uid Gid Home 216*0Sstevel@tonic-gate ------------------------------------------------------------------ 217*0Sstevel@tonic-gate . 218*0Sstevel@tonic-gate format STDOUT = 219*0Sstevel@tonic-gate @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<< 220*0Sstevel@tonic-gate $name, $login, $office,$uid,$gid, $home 221*0Sstevel@tonic-gate . 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate # a report from a bug report form 225*0Sstevel@tonic-gate format STDOUT_TOP = 226*0Sstevel@tonic-gate Bug Reports 227*0Sstevel@tonic-gate @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> 228*0Sstevel@tonic-gate $system, $%, $date 229*0Sstevel@tonic-gate ------------------------------------------------------------------ 230*0Sstevel@tonic-gate . 231*0Sstevel@tonic-gate format STDOUT = 232*0Sstevel@tonic-gate Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 233*0Sstevel@tonic-gate $subject 234*0Sstevel@tonic-gate Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 235*0Sstevel@tonic-gate $index, $description 236*0Sstevel@tonic-gate Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 237*0Sstevel@tonic-gate $priority, $date, $description 238*0Sstevel@tonic-gate From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 239*0Sstevel@tonic-gate $from, $description 240*0Sstevel@tonic-gate Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 241*0Sstevel@tonic-gate $programmer, $description 242*0Sstevel@tonic-gate ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 243*0Sstevel@tonic-gate $description 244*0Sstevel@tonic-gate ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 245*0Sstevel@tonic-gate $description 246*0Sstevel@tonic-gate ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 247*0Sstevel@tonic-gate $description 248*0Sstevel@tonic-gate ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< 249*0Sstevel@tonic-gate $description 250*0Sstevel@tonic-gate ~ ^<<<<<<<<<<<<<<<<<<<<<<<... 251*0Sstevel@tonic-gate $description 252*0Sstevel@tonic-gate . 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gateIt is possible to intermix print()s with write()s on the same output 255*0Sstevel@tonic-gatechannel, but you'll have to handle C<$-> (C<$FORMAT_LINES_LEFT>) 256*0Sstevel@tonic-gateyourself. 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate=head2 Format Variables 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gateThe current format name is stored in the variable C<$~> (C<$FORMAT_NAME>), 261*0Sstevel@tonic-gateand the current top of form format name is in C<$^> (C<$FORMAT_TOP_NAME>). 262*0Sstevel@tonic-gateThe current output page number is stored in C<$%> (C<$FORMAT_PAGE_NUMBER>), 263*0Sstevel@tonic-gateand the number of lines on the page is in C<$=> (C<$FORMAT_LINES_PER_PAGE>). 264*0Sstevel@tonic-gateWhether to autoflush output on this handle is stored in C<$|> 265*0Sstevel@tonic-gate(C<$OUTPUT_AUTOFLUSH>). The string output before each top of page (except 266*0Sstevel@tonic-gatethe first) is stored in C<$^L> (C<$FORMAT_FORMFEED>). These variables are 267*0Sstevel@tonic-gateset on a per-filehandle basis, so you'll need to select() into a different 268*0Sstevel@tonic-gateone to affect them: 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate select((select(OUTF), 271*0Sstevel@tonic-gate $~ = "My_Other_Format", 272*0Sstevel@tonic-gate $^ = "My_Top_Format" 273*0Sstevel@tonic-gate )[0]); 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gatePretty ugly, eh? It's a common idiom though, so don't be too surprised 276*0Sstevel@tonic-gatewhen you see it. You can at least use a temporary variable to hold 277*0Sstevel@tonic-gatethe previous filehandle: (this is a much better approach in general, 278*0Sstevel@tonic-gatebecause not only does legibility improve, you now have intermediary 279*0Sstevel@tonic-gatestage in the expression to single-step the debugger through): 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate $ofh = select(OUTF); 282*0Sstevel@tonic-gate $~ = "My_Other_Format"; 283*0Sstevel@tonic-gate $^ = "My_Top_Format"; 284*0Sstevel@tonic-gate select($ofh); 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gateIf you use the English module, you can even read the variable names: 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate use English '-no_match_vars'; 289*0Sstevel@tonic-gate $ofh = select(OUTF); 290*0Sstevel@tonic-gate $FORMAT_NAME = "My_Other_Format"; 291*0Sstevel@tonic-gate $FORMAT_TOP_NAME = "My_Top_Format"; 292*0Sstevel@tonic-gate select($ofh); 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gateBut you still have those funny select()s. So just use the FileHandle 295*0Sstevel@tonic-gatemodule. Now, you can access these special variables using lowercase 296*0Sstevel@tonic-gatemethod names instead: 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate use FileHandle; 299*0Sstevel@tonic-gate format_name OUTF "My_Other_Format"; 300*0Sstevel@tonic-gate format_top_name OUTF "My_Top_Format"; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gateMuch better! 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate=head1 NOTES 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gateBecause the values line may contain arbitrary expressions (for at fields, 307*0Sstevel@tonic-gatenot caret fields), you can farm out more sophisticated processing 308*0Sstevel@tonic-gateto other functions, like sprintf() or one of your own. For example: 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate format Ident = 311*0Sstevel@tonic-gate @<<<<<<<<<<<<<<< 312*0Sstevel@tonic-gate &commify($n) 313*0Sstevel@tonic-gate . 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gateTo get a real at or caret into the field, do this: 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate format Ident = 318*0Sstevel@tonic-gate I have an @ here. 319*0Sstevel@tonic-gate "@" 320*0Sstevel@tonic-gate . 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gateTo center a whole line of text, do something like this: 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate format Ident = 325*0Sstevel@tonic-gate @||||||||||||||||||||||||||||||||||||||||||||||| 326*0Sstevel@tonic-gate "Some text line" 327*0Sstevel@tonic-gate . 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gateThere is no builtin way to say "float this to the right hand side 330*0Sstevel@tonic-gateof the page, however wide it is." You have to specify where it goes. 331*0Sstevel@tonic-gateThe truly desperate can generate their own format on the fly, based 332*0Sstevel@tonic-gateon the current number of columns, and then eval() it: 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate $format = "format STDOUT = \n" 335*0Sstevel@tonic-gate . '^' . '<' x $cols . "\n" 336*0Sstevel@tonic-gate . '$entry' . "\n" 337*0Sstevel@tonic-gate . "\t^" . "<" x ($cols-8) . "~~\n" 338*0Sstevel@tonic-gate . '$entry' . "\n" 339*0Sstevel@tonic-gate . ".\n"; 340*0Sstevel@tonic-gate print $format if $Debugging; 341*0Sstevel@tonic-gate eval $format; 342*0Sstevel@tonic-gate die $@ if $@; 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gateWhich would generate a format looking something like this: 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate format STDOUT = 347*0Sstevel@tonic-gate ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 348*0Sstevel@tonic-gate $entry 349*0Sstevel@tonic-gate ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ 350*0Sstevel@tonic-gate $entry 351*0Sstevel@tonic-gate . 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gateHere's a little program that's somewhat like fmt(1): 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate format = 356*0Sstevel@tonic-gate ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ 357*0Sstevel@tonic-gate $_ 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate . 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate $/ = ''; 362*0Sstevel@tonic-gate while (<>) { 363*0Sstevel@tonic-gate s/\s*\n\s*/ /g; 364*0Sstevel@tonic-gate write; 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate=head2 Footers 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gateWhile $FORMAT_TOP_NAME contains the name of the current header format, 370*0Sstevel@tonic-gatethere is no corresponding mechanism to automatically do the same thing 371*0Sstevel@tonic-gatefor a footer. Not knowing how big a format is going to be until you 372*0Sstevel@tonic-gateevaluate it is one of the major problems. It's on the TODO list. 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gateHere's one strategy: If you have a fixed-size footer, you can get footers 375*0Sstevel@tonic-gateby checking $FORMAT_LINES_LEFT before each write() and print the footer 376*0Sstevel@tonic-gateyourself if necessary. 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gateHere's another strategy: Open a pipe to yourself, using C<open(MYSELF, "|-")> 379*0Sstevel@tonic-gate(see L<perlfunc/open()>) and always write() to MYSELF instead of STDOUT. 380*0Sstevel@tonic-gateHave your child process massage its STDIN to rearrange headers and footers 381*0Sstevel@tonic-gatehowever you like. Not very convenient, but doable. 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate=head2 Accessing Formatting Internals 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gateFor low-level access to the formatting mechanism. you may use formline() 386*0Sstevel@tonic-gateand access C<$^A> (the $ACCUMULATOR variable) directly. 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gateFor example: 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate $str = formline <<'END', 1,2,3; 391*0Sstevel@tonic-gate @<<< @||| @>>> 392*0Sstevel@tonic-gate END 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate print "Wow, I just stored `$^A' in the accumulator!\n"; 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gateOr to make an swrite() subroutine, which is to write() what sprintf() 397*0Sstevel@tonic-gateis to printf(), do this: 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate use Carp; 400*0Sstevel@tonic-gate sub swrite { 401*0Sstevel@tonic-gate croak "usage: swrite PICTURE ARGS" unless @_; 402*0Sstevel@tonic-gate my $format = shift; 403*0Sstevel@tonic-gate $^A = ""; 404*0Sstevel@tonic-gate formline($format,@_); 405*0Sstevel@tonic-gate return $^A; 406*0Sstevel@tonic-gate } 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate $string = swrite(<<'END', 1, 2, 3); 409*0Sstevel@tonic-gate Check me out 410*0Sstevel@tonic-gate @<<< @||| @>>> 411*0Sstevel@tonic-gate END 412*0Sstevel@tonic-gate print $string; 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate=head1 WARNINGS 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gateThe lone dot that ends a format can also prematurely end a mail 417*0Sstevel@tonic-gatemessage passing through a misconfigured Internet mailer (and based on 418*0Sstevel@tonic-gateexperience, such misconfiguration is the rule, not the exception). So 419*0Sstevel@tonic-gatewhen sending format code through mail, you should indent it so that 420*0Sstevel@tonic-gatethe format-ending dot is not on the left margin; this will prevent 421*0Sstevel@tonic-gateSMTP cutoff. 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gateLexical variables (declared with "my") are not visible within a 424*0Sstevel@tonic-gateformat unless the format is declared within the scope of the lexical 425*0Sstevel@tonic-gatevariable. (They weren't visible at all before version 5.001.) 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gateFormats are the only part of Perl that unconditionally use information 428*0Sstevel@tonic-gatefrom a program's locale; if a program's environment specifies an 429*0Sstevel@tonic-gateLC_NUMERIC locale, it is always used to specify the decimal point 430*0Sstevel@tonic-gatecharacter in formatted output. Perl ignores all other aspects of locale 431*0Sstevel@tonic-gatehandling unless the C<use locale> pragma is in effect. Formatted output 432*0Sstevel@tonic-gatecannot be controlled by C<use locale> because the pragma is tied to the 433*0Sstevel@tonic-gateblock structure of the program, and, for historical reasons, formats 434*0Sstevel@tonic-gateexist outside that block structure. See L<perllocale> for further 435*0Sstevel@tonic-gatediscussion of locale handling. 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gateWithin strings that are to be displayed in a fixed length text field, 438*0Sstevel@tonic-gateeach control character is substituted by a space. (But remember the 439*0Sstevel@tonic-gatespecial meaning of C<\r> when using fill mode.) This is done to avoid 440*0Sstevel@tonic-gatemisalignment when control characters "disappear" on some output media. 441*0Sstevel@tonic-gate 442