Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data.
Define a Format
Following is the syntax to define a Perl format
Here 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.
Next fieldline can contain any text or fieldholders. The fieldholders hold space for data that will be placed there at a later date.
@<<<<
: 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.
@>>>>
: This placeholder is used for right-justified.
@||||
: This placeholder is used for centered
@####.##
: numeric field holder
@*
: multiline field holder
In this example, $name would be written as left justify within 22 character spaces and after that age will be written in two spaces.
Using the Format
In order to invoke this format declaration, we would use the write
keyword as
write EMPLOYEE;
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.
select(STDOUT);
We would then associate EMPLOYEE with STDOUT by setting the new
format name with STDOUT, using the special variable $~ or
$FORMAT_NAME as follows -
$~ = "EMPLOYEE";
When we now do a write(), the data would be sent to STDOUT. Remember: if you are going to write your report in any other file handle instead of STDOUT then you can use select() function to select that file handle and rest of the logic will remain the same.
Let's take the following example. Here we have hard coded values just for showing the usage. In actual usage you will read values from a file or database to generate actual reports and you may need to write final report again into a file.
When executed, this will produce the following result
Define 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 and assign it to $^ or $FORMAT_TOP_NAME variable
Now your report will look like
Define a Pagination
What about if your report is taking more than one page? You have a solution for that, simply use $% or $FORMAT_PAGE_NUMBER vairable along with header as follows
Now your output will look like as follows
Number of Lines on a Page
You can set the number of lines per page using special variable $= ( or $FORMAT_LINES_PER_PAGE ), By default $= will be 60.
Define a Report Footer
While $^ or $FORMAT_TOP_NAME contains the name of the current header format, there is no corresponding mechanism to automatically do the same thing for a footer. If you have a fixed-size footer, you can get footers by checking variable $- or $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary using another format defined as follows -
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.