Perl Information Center Tutorials - Report Writing
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Reports
Perl provides the capability of generating simple reports and charts.
The key to creating a report is to use the format function, which allows
a programmer creates a template (called a picture in Perl) of how the
report needs to look. Once a format is defined, the write function is
used to print the report.
The format function syntax is given next, followed by an example
format and the report it provides.
format NAME = # format name (typically same as filehandle
FORMLIST # the template - fields/values
. # single . terminates a format
Then, the write function is used to print the report using the
following syntax.
write FILEHANDLE
The format NAME is typically the same as the name of the FILEHANDLE,
although Perl has methods for associating a specific format with
a differently name filehandle by using one of its special variables.
Report Example
In this example, a one-line report format is defined which prints
three variables. Then the Perl program assigns values to each
variable and uses the write function to print the report to
the file 'myreport.txt'
format OUTFILE =
Test: @<<<<<<<< @||||| @>>>>>
$x, $y, $z
.
$x = "dog"; $y = "cat"; $z = "pig";
open ">myreport.txt";
write OUTFILE;
OUTPUT:
Test: dog cat pig
FORMLIST
The FORMLIST section of a format consist of 3 types of lines:
- Comment - # in the first column
- Picture - format for a single line of output
- Arguments - values/variables to place in previous picture line
Picture Lines
Text that you type into picture lines is reproduced exactly as you type it,
except for areas designated for data (called fields). The following characters
are used in a picture line to produce the indicated results.
@ start of regular field
^ start of special field
< pad character for left adjustification
| pad character for centering
> pad character for right adjustificat
# 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
Example:
@<<<<< @|||| @>>>> @#.## # single picture line with 4 fields
Arguments (Field Values)
Values for fields must be on the next line following the field definitions.
Position of the values is not critical, but must be separated by commas
as in the following example.
@<<<<< @|||| @>>>> # single picture line with 3 fields
$x, $y, $z # 3 variables to put in the fields
Numeric Fields
The @ and # characters may be combined to represent how a number will print
out, as in the following examples.
@### 42
@##.### 12.218
Other Supported Field Types
The Perl format also supports multiline fields and filling fields. See
perldoc for more information.
If you have any suggestions or correction, please let me know.
|