As stated earlier that Perl stands for Practical Extraction and Reporting Language, and we’ll now discuss using Perl to write reports.
Perl uses a writing template called a ‘format’ to output reports. To use the format feature of Perl, you must:
  • Define a Format
  • Pass the data that will be displayed on the format
  • Invoke the Format

Define a Format

Following is the syntax to define a Perl format
FormatName represents the name of the format. The fieldline is the specific way the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period.
fieldline can contain any text or fieldholders. Fieldholders hold space for data that will be placed there at a later date. A fieldholder has the format:
This fieldholder is left-justified, with a field space of 5. You must count the @ sign and the < signs to know the number of spaces in the field. Other field holders include:
An example format would be:
In this example $name would be written as left justify within 22 character spaces and after that age will be written in two spaces.

Invoke the Format to write Data

In order to invoke this format declaration we would use the write keyword:
The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function
We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~
When we now do a write(), the dat

a would be sent to STDOUT. Remember: if you didn’t have STDOUT set as your default file handle, you could revert back to the original file handle by assigning the return value of select to a scalar value, and using select along with this scalar variable after the special variable is assigned the format name, to be associated with STDOUT.

The above example will generate a report in the following format



Defining a Report Header

Everything looks fine. But you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this. Apart from defining a template you would have to define a header which will have same name but appended with _TOP keyword as follows
Now your report will look like


Defining a Pagination & Number of Lines on a Page

What about if your report is taking more than one page ? You have a solution for that. Use $%vairable along with header as follows
Now your output will look like
You can set the number of lines per page using special variable $= ( or $FORMAT_LINES_PER_PAGE ) By default $= will be 60

Defining a Report Footer

One final thing is left which is footer. Very similar to header, you can define a footer and it will be written after each page. Here you will use _BOTTOM keyword instead of _TOP.
This will give you following result
For a complete set of variables related to formating, please refer to Perl Special Variablessection.

What Do You Think on This ? Say Here

This site uses Akismet to reduce spam. Learn how your comment data is processed.