Formats, like packages and subroutines, are declared rather than executed, so they may occur at any point in your program. (Usually it's best to keep them all together though.) They have their own namespace apart from all the other ``types'' in Perl. This means that if you have a function named ``Foo'', it is not the same thing as having a format named ``Foo''. However, the default name for the format associated with a given filehandle is the same as the name of the filehandle. Thus, the default format for STDOUT is named ``STDOUT'', and the default format for filehandle TEMP is named ``TEMP''. They just look the same. They aren't.
Output record formats are declared as follows:
format NAME = FORMLIST .
If the name is omitted, format ``STDOUT'' is defined. A single ``.'' in column 1 is used to terminate a format. FORMLIST consists of a sequence of lines, each of which may be one of three types:
Picture lines contain output field definitions, intermingled with literal text. These lines do not undergo any kind of variable interpolation. Field definitions are made up from a set of characters, for starting and extending a field to its desired width. This is the complete set of characters for field definitions:
@ start of regular field ^ start of special field < pad character for left justification | pad character for centering > pad character for right justification # pad character for a right-justified numeric field 0 instead of first #: pad number with leading zeroes . decimal point within a numeric field ... terminate a text field, show "..." as truncation evidence @* variable width field for a multi-line value ^* variable width field for next line of a multi-line value ~ suppress line with all fields empty ~~ repeat line until all fields are exhausted
Each field in a picture line starts with either ``@'' (at) or ``^'' (caret), indicating what we'll call, respectively, a ``regular'' or ``special'' field. The choice of pad characters determines whether a field is textual or numeric. The tilde operators are not part of a field. Let's look at the various possibilities in detail.
Example: format STDOUT = @<<<<<< @|||||| @>>>>>> "left", "middle", "right" . Output: left middle right
Example: format STDOUT = @### @.### @##.### @### @### ^#### 42, 3.1415, undef, 0, 10000, undef . Output: 42 3.142 0.000 0 ####
Example: $text = "line 1\nline 2\nline 3"; format STDOUT = Text: ^* $text ~~ ^* $text . Output: Text: line 1 line 2 line 3
Normally you would use a sequence of fields in a vertical stack associated with the same scalar variable to print out a block of text. You might wish to end the final field with the text ``...'', which will appear in the output if the text was too long to appear in its entirety.
Examples:
# a report on the /etc/passwd file format STDOUT_TOP = Passwd File Name Login Office Uid Gid Home ------------------------------------------------------------------ . format STDOUT = @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<< $name, $login, $office,$uid,$gid, $home . # a report from a bug report form format STDOUT_TOP = Bug Reports @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> $system, $%, $date ------------------------------------------------------------------ . format STDOUT = Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $subject Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $index, $description Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $priority, $date, $description From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $from, $description Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $programmer, $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<... $description .
It is possible to intermix print()s with write()s on the same output channel, but you'll have to handle "$-" ($FORMAT_LINES_LEFT) yourself.
select((select(OUTF), $~ = "My_Other_Format", $^ = "My_Top_Format" )[0]);
Pretty ugly, eh? It's a common idiom though, so don't be too surprised when you see it. You can at least use a temporary variable to hold the previous filehandle: (this is a much better approach in general, because not only does legibility improve, you now have an intermediary stage in the expression to single-step the debugger through):
$ofh = select(OUTF); $~ = "My_Other_Format"; $^ = "My_Top_Format"; select($ofh);
If you use the English module, you can even read the variable names:
use English; $ofh = select(OUTF); $FORMAT_NAME = "My_Other_Format"; $FORMAT_TOP_NAME = "My_Top_Format"; select($ofh);
But you still have those funny select()s. So just use the FileHandle module. Now, you can access these special variables using lowercase method names instead:
use FileHandle; format_name OUTF "My_Other_Format"; format_top_name OUTF "My_Top_Format";
format Ident = @<<<<<<<<<<<<<<< &commify($n) .
To get a real at or caret into the field, do this:
format Ident = I have an @ here. "@" .
To center a whole line of text, do something like this:
format Ident = @||||||||||||||||||||||||||||||||||||||||||||||| "Some text line" .
There is no builtin way to say ``float this to the right hand side of the page, however wide it is.'' You have to specify where it goes. The truly desperate can generate their own format on the fly, based on the current number of columns, and then eval() it:
$format = "format STDOUT = \n" . '^' . '<' x $cols . "\n" . '$entry' . "\n" . "\t^" . "<" x ($cols-8) . "~~\n" . '$entry' . "\n" . ".\n"; print $format if $Debugging; eval $format; die $@ if $@;
Which would generate a format looking something like this:
format STDOUT = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $entry ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ $entry .
Here's a little program that's somewhat like fmt(1):
format = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ $_ . $/ = ''; while (<>) { s/\s*\n\s*/ /g; write; }
Here's one strategy: If you have a fixed-size footer, you can get footers by checking $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary.
Here's another strategy: Open a pipe to yourself, using "open(MYSELF, "|-")" (see ``open'' in perlfunc) and always write() to MYSELF instead of STDOUT. Have your child process massage its STDIN to rearrange headers and footers however you like. Not very convenient, but doable.
For example:
$str = formline <<'END', 1,2,3; @<<< @||| @>>> END print "Wow, I just stored '$^A' in the accumulator!\n";
Or to make an swrite() subroutine, which is to write() what sprintf() is to printf(), do this:
use Carp; sub swrite { croak "usage: swrite PICTURE ARGS" unless @_; my $format = shift; $^A = ""; formline($format,@_); return $^A; } $string = swrite(<<'END', 1, 2, 3); Check me out @<<< @||| @>>> END print $string;
Lexical variables (declared with ``my'') are not visible within a format unless the format is declared within the scope of the lexical variable.
If a program's environment specifies an LC_NUMERIC locale and "use locale" is in effect when the format is declared, the locale is used to specify the decimal point character in formatted output. Formatted output cannot be controlled by "use locale" at the time when write() is called. See perllocale for further discussion of locale handling.
Within strings that are to be displayed in a fixed-length text field, each control character is substituted by a space. (But remember the special meaning of "\r" when using fill mode.) This is done to avoid misalignment when control characters ``disappear'' on some output media.