Cari di Perl 
    Perl Tutorial
Daftar Isi
(Sebelumnya) Reference pointer data structu ...Object OOP (Berikutnya)
Language Reference

Format report chart

Daftar Isi

NAME

perlform - Perl formats

DESCRIPTION

Perl has a mechanism to help you generate simple reports and charts. Tofacilitate this, Perl helps you code up your output page close to how itwill look when it's printed. It can keep track of things like how manylines are on a page, what page you're on, when to print page headers,etc. Keywords are borrowed from FORTRAN: format() to declare and write()to execute; see their entries in perlfunc. Fortunately, the layout ismuch more legible, more like BASIC's PRINT USING statement. Think of itas a poor man's nroff(1).

Formats, like packages and subroutines, are declared rather thanexecuted, so they may occur at any point in your program. (Usually it'sbest to keep them all together though.) They have their own namespaceapart from all the other "types" in Perl. This means that if you have afunction named "Foo", it is not the same thing as having a format named"Foo". However, the default name for the format associated with a givenfilehandle is the same as the name of the filehandle. Thus, the defaultformat for STDOUT is named "STDOUT", and the default format for filehandleTEMP is named "TEMP". They just look the same. They aren't.

Output record formats are declared as follows:

  1. format NAME =
  2. FORMLIST
  3. .

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:

1.

A comment, indicated by putting a '#' in the first column.

2.

A "picture" line giving the format for one output line.

3.

An argument line supplying values to plug into the previous picture line.

Picture lines contain output field definitions, intermingled withliteral text. These lines do not undergo any kind of variable interpolation.Field definitions are made up from a set of characters, for starting andextending a field to its desired width. This is the complete set ofcharacters for field definitions:

  1. @ start of regular field
  2. ^ start of special field
  3. < pad character for left justification
  4. | pad character for centering
  5. > pad character for right justification
  6. # pad character for a right-justified numeric field
  7. 0 instead of first #: pad number with leading zeroes
  8. . decimal point within a numeric field
  9. ... terminate a text field, show "..." as truncation evidence
  10. @* variable width field for a multi-line value
  11. ^* variable width field for next line of a multi-line value
  12. ~ suppress line with all fields empty
  13. ~~ 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 ornumeric. The tilde operators are not part of a field. Let's look atthe various possibilities in detail.

Text Fields

The length of the field is supplied by padding out the field with multiple "<", ">", or "|" characters to specify a non-numeric field with,respectively, left justification, right justification, or centering. For a regular field, the value (up to the first newline) is taken andprinted according to the selected justification, truncating excess characters.If you terminate a text field with "...", three dots will be shown ifthe value is truncated. A special text field may be used to do rudimentary multi-line text block filling; see Using Fill Mode for details.

  1. Example:
  2. format STDOUT =
  3. @<<<<<< @|||||| @>>>>>>
  4. "left", "middle", "right"
  5. .
  6. Output:
  7. left middle right

Numeric Fields

Using "#" as a padding character specifies a numeric field, withright justification. An optional "." defines the position of thedecimal point. With a "0" (zero) instead of the first "#", theformatted number will be padded with leading zeroes if necessary.A special numeric field is blanked out if the value is undefined.If the resulting value would exceed the width specified the field isfilled with "#" as overflow evidence.

  1. Example:
  2. format STDOUT =
  3. @### @.### @##.### @### @### ^####
  4. 42, 3.1415, undef, 0, 10000, undef
  5. .
  6. Output:
  7. 42 3.142 0.000 0 ####

The Field @* for Variable-Width Multi-Line Text

The field "@*" can be used for printing multi-line, nontruncatedvalues; it should (but need not) appear by itself on a line. A finalline feed is chomped off, but all other characters are emitted verbatim.

The Field ^* for Variable-Width One-line-at-a-time Text

Like "@*", this is a variable-width field. The value supplied must be a scalar variable. Perl puts the first line (up to the first "\n") of the text into the field, and then chops off the front of the string so that the next time the variable is referenced, more of the text can be printed. The variable will not be restored.

  1. Example:
  2. $text = "line 1\nline 2\nline 3";
  3. format STDOUT =
  4. Text: ^*
  5. $text
  6. ~~ ^*
  7. $text
  8. .
  9. Output:
  10. Text: line 1
  11. line 2
  12. line 3

Specifying Values

The values are specified on the following format line in the same order asthe picture fields. The expressions providing the values must beseparated by commas. They are all evaluated in a list contextbefore the line is processed, so a single list expression could producemultiple list elements. The expressions may be spread out to more thanone line if enclosed in braces. If so, the opening brace must be the firsttoken on the first line. If an expression evaluates to a number with adecimal part, and if the corresponding picture specifies that the decimalpart should appear in the output (that is, any picture except multiple "#"characters without an embedded "."), the character used for the decimalpoint is determined by the current LC_NUMERIC locale if use locale is ineffect. This means that, if, for example, the run-time environment happensto specify a German locale, "," will be used instead of the default ".". Seeperllocale and WARNINGS for more information.

Using Fill Mode

On text fields the caret enables a kind of fill mode. Instead of anarbitrary expression, the value supplied must be a scalar variablethat contains a text string. Perl puts the next portion of the text intothe field, and then chops off the front of the string so that the next timethe variable is referenced, more of the text can be printed. (Yes, thismeans that the variable itself is altered during execution of the write()call, and is not restored.) The next portion of text is determined bya crude line-breaking algorithm. You may use the carriage return character(\r) to force a line break. You can change which characters are legal to break on by changing the variable $: (that's $FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a list of the desired characters.

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.

Suppressing Lines Where All Fields Are Void

Using caret fields can produce lines where all fields are blank. You cansuppress such lines by putting a "~" (tilde) character anywhere in theline. The tilde will be translated to a space upon output.

Repeating Format Lines

If you put two contiguous tilde characters "~~" anywhere into a line,the line will be repeated until all the fields on the line are exhausted,i.e. undefined. For special (caret) text fields this will occur sooner orlater, but if you use a text field of the at variety, the expression yousupply had better not give the same value every time forever! (shift(@f)is a simple example that would work.) Don't use a regular (at) numeric field in such lines, because it will never go blank.

Top of Form Processing

Top-of-form processing is by default handled by a format with thesame name as the current filehandle with "_TOP" concatenated to it.It's triggered at the top of each page. See write.

Examples:

  1. # a report on the /etc/passwd file
  2. format STDOUT_TOP =
  3. Passwd File
  4. Name Login Office Uid Gid Home
  5. ------------------------------------------------------------------
  6. .
  7. format STDOUT =
  8. @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
  9. $name, $login, $office,$uid,$gid, $home
  10. .
  11. # a report from a bug report form
  12. format STDOUT_TOP =
  13. Bug Reports
  14. @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>
  15. $system, $%, $date
  16. ------------------------------------------------------------------
  17. .
  18. format STDOUT =
  19. Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  20. $subject
  21. Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  22. $index, $description
  23. Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  24. $priority, $date, $description
  25. From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  26. $from, $description
  27. Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  28. $programmer, $description
  29. ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  30. $description
  31. ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  32. $description
  33. ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  34. $description
  35. ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  36. $description
  37. ~ ^<<<<<<<<<<<<<<<<<<<<<<<...
  38. $description
  39. .

It is possible to intermix print()s with write()s on the same outputchannel, but you'll have to handle $- ($FORMAT_LINES_LEFT)yourself.

Format Variables

The current format name is stored in the variable $~ ($FORMAT_NAME),and the current top of form format name is in $^ ($FORMAT_TOP_NAME).The current output page number is stored in $% ($FORMAT_PAGE_NUMBER),and the number of lines on the page is in $= ($FORMAT_LINES_PER_PAGE).Whether to autoflush output on this handle is stored in $|($OUTPUT_AUTOFLUSH). The string output before each top of page (exceptthe first) is stored in $^L ($FORMAT_FORMFEED). These variables areset on a per-filehandle basis, so you'll need to select() into a differentone to affect them:

  1. select((select(OUTF),
  2. $~ = "My_Other_Format",
  3. $^ = "My_Top_Format"
  4. )[0]);

Pretty ugly, eh? It's a common idiom though, so don't be too surprisedwhen you see it. You can at least use a temporary variable to holdthe previous filehandle: (this is a much better approach in general,because not only does legibility improve, you now have an intermediarystage in the expression to single-step the debugger through):

  1. $ofh = select(OUTF);
  2. $~ = "My_Other_Format";
  3. $^ = "My_Top_Format";
  4. select($ofh);

If you use the English module, you can even read the variable names:

  1. use English '-no_match_vars';
  2. $ofh = select(OUTF);
  3. $FORMAT_NAME = "My_Other_Format";
  4. $FORMAT_TOP_NAME = "My_Top_Format";
  5. select($ofh);

But you still have those funny select()s. So just use the FileHandlemodule. Now, you can access these special variables using lowercasemethod names instead:

  1. use FileHandle;
  2. format_name OUTF "My_Other_Format";
  3. format_top_name OUTF "My_Top_Format";

Much better!

NOTES

Because the values line may contain arbitrary expressions (for at fields,not caret fields), you can farm out more sophisticated processingto other functions, like sprintf() or one of your own. For example:

  1. format Ident =
  2. @<<<<<<<<<<<<<<<
  3. &commify($n)
  4. .

To get a real at or caret into the field, do this:

  1. format Ident =
  2. I have an @ here.
  3. "@"
  4. .

To center a whole line of text, do something like this:

  1. format Ident =
  2. @|||||||||||||||||||||||||||||||||||||||||||||||
  3. "Some text line"
  4. .

There is no builtin way to say "float this to the right hand sideof the page, however wide it is." You have to specify where it goes.The truly desperate can generate their own format on the fly, basedon the current number of columns, and then eval() it:

  1. $format = "format STDOUT = \n"
  2. . '^' . '<' x $cols . "\n"
  3. . '$entry' . "\n"
  4. . "\t^" . "<" x ($cols-8) . "~~\n"
  5. . '$entry' . "\n"
  6. . ".\n";
  7. print $format if $Debugging;
  8. eval $format;
  9. die $@ if $@;

Which would generate a format looking something like this:

  1. format STDOUT =
  2. ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  3. $entry
  4. ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
  5. $entry
  6. .

Here's a little program that's somewhat like fmt(1):

  1. format =
  2. ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
  3. $_
  4. .
  5. $/ = '';
  6. while (<>) {
  7. s/\s*\n\s*/ /g;
  8. write;
  9. }

Footers

While $FORMAT_TOP_NAME contains the name of the current header format,there is no corresponding mechanism to automatically do the same thingfor a footer. Not knowing how big a format is going to be until youevaluate it is one of the major problems. It's on the TODO list.

Here's one strategy: If you have a fixed-size footer, you can get footersby checking $FORMAT_LINES_LEFT before each write() and print the footeryourself if necessary.

Here's another strategy: Open a pipe to yourself, using open(MYSELF, "|-")(see open) and always write() to MYSELF instead of STDOUT.Have your child process massage its STDIN to rearrange headers and footershowever you like. Not very convenient, but doable.

Accessing Formatting Internals

For low-level access to the formatting mechanism. you may use formline()and access $^A (the $ACCUMULATOR variable) directly.

For example:

  1. $str = formline <<'END', 1,2,3;
  2. @<<< @||| @>>>
  3. END
  4. 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:

  1. use Carp;
  2. sub swrite {
  3. croak "usage: swrite PICTURE ARGS" unless @_;
  4. my $format = shift;
  5. $^A = "";
  6. formline($format,@_);
  7. return $^A;
  8. }
  9. $string = swrite(<<'END', 1, 2, 3);
  10. Check me out
  11. @<<< @||| @>>>
  12. END
  13. print $string;

WARNINGS

The lone dot that ends a format can also prematurely end a mailmessage passing through a misconfigured Internet mailer (and based onexperience, such misconfiguration is the rule, not the exception). Sowhen sending format code through mail, you should indent it so thatthe format-ending dot is not on the left margin; this will preventSMTP cutoff.

Lexical variables (declared with "my") are not visible within aformat unless the format is declared within the scope of the lexicalvariable. (They weren't visible at all before version 5.001.)

If a program's environment specifies an LC_NUMERIC locale and uselocale is in effect when the format is declared, the locale is usedto specify the decimal point character in formatted output. Formattedoutput 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 thespecial meaning of \r when using fill mode.) This is done to avoidmisalignment when control characters "disappear" on some output media.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Reference pointer data structu ...Object OOP (Berikutnya)