PERL stands for Practical Extraction and Reporting Language, and this last part or the meaning is what we will cover. How to use perl in order to write reports.
PERL uses a writing tempalte called a format in order to output reports. When you want to use the format feature of perl you must
a) Definethe Format
b) Pass the data that will be displayed on the format
c) Invoke the Format
You define the format using the format definition in the following manner:
format FormatName =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
.
FormatName would be the name of the format, the fieldline is the specific way the data should be formated and the values line are the values that will go in the field line. You end the format by a single period.
fieldline can contain any text and they can also contain fieldholders, fieldholders refer to the the data that will be placed there, a fieldholder has the format:
@<<<<
Which is left justified with a field space of 5, count the @ sign and the < signs to know the number of spaces. Other field holders are:
@>>>> : right justified
@|||| : centered
@####.## : numeric field holder
@* : multiline field holder
An example format would be:
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.
We can see that only scalar variables can be used and not arrays or hashes. In order to invoke this format declaration we would use the write keyword:
write EMPLOYEE; #send to the output
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, since we want the data sent to the STDOUT we would have to associate EMPLOYEE with the STDOUT filehandle. What we would have to do first is to make sure that that STDOUT is our selected file handle usign the select() function:
select(STDOUT);
We would then associate EMPLOYEE with STDOUT by setting the new format name with the STDOUT using the special variable $~ :
$~ = "EMPLOYEE";
When we now do a write(), the data would be sent to STDOUT. Remember that if you did not have STDOUT set as your default file handle, then you can 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.
I hope that this small introduction to perl has helped you. For information
on PERL 5 please go to the tutorial index and go to the PERL 5 section..
![]() |
![]() |