Wednesday, August 29, 2007

Simple Report Formats

#!/usr/local/bin/perl
#
# Open a file create/write
# Print out a simple formatted report
#

$col_1="column one text 789_12345" ;
$col_2="column two text 789_12345" ;
$col_3=123456789.12345 ;

$note_1 = "Note 1: The code segment demonstrates the method of generating a formatted report using several variables containing text and numbers. " ;

$note_2 = "Note 2: The code segment demonstrates the method of generating a formatted report using several variables containing text and numbers. " ;

$note_3 = "Note 3: The code segment demonstrates \n the method of generating a \n formatted report using several \n variables containing text \n and numbers. " ;

open ( REPORT_FILE, ">report.txt" ) || die " report.txt $! \n " ;

# a dot marks the end of the format
format REPORT_FILE =
123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456
+--------------------------------------------------------------------------+
| Top of Report |
+--------------------------------------------------------------------------+
|Column One |Column Two |Column Three |
+------------------------+------------------------+------------------------+
| | | |
| @<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<< | @##################.## |
$col_1, $col_2, $col_3
| @<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<< | @##################.## |
$col_1, $col_2, $col_3
| @<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<< | @##################.## |
$col_1, $col_2, $col_3
| | | |
+------------------------+------------------------+------------------------+
^|||||||||||||||||||||||||||||||||||||||
$note_1
~ ^|||||||||||||||||||||||||||||||||||||||
$note_1
~ ^|||||||||||||||||||||||||||||||||||||||
$note_1
~ ^|||||||||||||||||||||||||||||||||||||||
$note_1
~ ^|||||||||||||||||||||||||||||||||||||||
$note_1

~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$note_2

@*
$note_3

+--------------------------------------------------------------------------+
.
# make sure the dot is in column one

write REPORT_FILE ;

print "\n\nALL DONE\n\n" ;
#
# DONE
#



The code segment demonstrates a method of generating a formatted report using several variables containing text and numbers.

An output file is opened for writing and assigned the file handle REPORT_FILE. The "|| die..." statement reads "or die and print ...". In this case, if the output file report.txt can not be opened for writing then the name of the file is printed to the STDERR (screen) device followed by the value of the special variable $! (error number or error string).

The formatting of a simple report begins with the statement format FILEHANDLE= and ends with a dot (period) on its own line then followed by the statement write FILEHANDLE. Everything in between is either text to be printed or format lines followed by variable lists.

The format lines contain fields starting with one of three symbols:

    @   (regular field),
@* (multi-line field),
^ (filling fields).
These start of field symbols are followed by one of five formatting symbols, each representing one character of text to be displayed:
    <   (left justified),
> (right justified),
| (centered),
# (numbers),
. (decimal placement).

$note_1 is spread out over three formatted lines, with breaks at word boundaries. The leading ~ symbol causes blank lines to be omitted, if there is more formatted space than characters to display. If there are more characters to display than alloted formatted space then the excess characters are omitted from the display. Otherwise, this text will be centered.

$note_2 is spread out over as many formatted lines as needed, with breaks at word boundaries. The leading ~~ symbols causes just enough formatted space to be created for the amount of text to be displayed. The format line is repeated as needed. This text will be centered as well.

$note_3 is spread out over lines, separated at the embedded \n new line special character. However, the hanging indent found in displaying $note_1 and $note_2 is not found in the display of $note_3. Only the first line is indented.

No comments: