Formatting forms in Perl

Walden Systems Geeks Corner Formatting forms in Perl tutorial programming developers Rutherford NJ New Jersey NYC New York North Bergen County
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

Perl stands for Practical Extraction and Report Language. In this tutorial, we learn about the report language part of Perl. Perl has a way to help us generate simple reports and charts. Perl helps us code our output page close to how it will look when it's printed. It can keep track of things like how many lines are on a page, what page we're on, when to print page headers, etc. To do this, Perl uses two FORTRAN keywords, format to declare and write to execute. Using a format consists of three things, defining a format, lading the data, and invoking the format.

Defining a format

A format is defined using a format definition. Formats, like packages and subroutines, are declared rather than executed, so we can add them at any point in our program. Formats have their own namespace from all the other types in Perl. What this means is that if we have a function named "Foo", it is not the same as having a format named "Foo". However, the default name for the format associated with a filehandle is the same as the name of the filehandle so the default format for STDOUT is named "STDOUT", and the default format for filehandle TEMP is named "TEMP". The format definition consists of a format name, field line and value. The format name is the name used to invoke the formatting. The field line is the way the data should be formatted. And finally, the value is what will be entered on the line. In the example below the format characters, we define a format named STUDENT that will will have 20 spaces for first name and last name and will left justify the last name. It will also have 2 spaces for age and grade. For more detailed explanation of fieldfolders, click here.

format characters

@	The start of a field holder.
<	The field should be left-justified.
>	The field should be right-justified.
|	The field should be centered.
#	The field will be numeric. If used as the first character in the line,
        The entire line is a comment.
.	A decimal point should be used with numeric fields.
^	Also represents the start of a field holder but tells Perl to turn on word-wrap mode.
~	The line should not be written if it is blank.
~~	Lines should be written as needed until there are no more values.
@*	A multi-line field will be used.



format STUDENT =
@<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<
$firstName $lastName
@## @##
$age $grade
.


Invoking a format

To invoke a format declaration, we use the write keyword. This function takes the name of a filehandle and generates text for that filehandle using the current format for that filehandle. 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. If we want the data sent to the STDOUT, we must associate the format definition with the STDOUT filehandle. Before we can do that, we must make sure that that STDOUT is our selected file handle, using the select( ) function. To associate the format definition with STDOUT, we use the special variable $~. After associating the format definition with STDOUT, we can now use the write( ) function to withe the report.

format STUDENT =
@<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<
$firstName $lastName
@## @##
$age $grade
.

select(STDOUT);
$~ = STUDENT;

@firstNames = ("John", "Jane", "Jack");
@lastNames = ("Doe", "Smith", "Jones" ) ;
@ages  = (11,15, 8);
@grades = (5 , 10, 3);

$i = 0;
foreach (@firstNames) 
{
   $fName = $_;
   $lName = $lastNames[$i];
   $age = $ages[$i];
   $grade = $grades[$i];
   $i++ ;
   write;
}


When executed, it will produce the following :
John                Doe
11  5
Jane                Smith
15 10
Jack                Jones
 8  3