How to Define a Format in Perl?

PERLServer Side ProgrammingProgramming Scripts

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.

Syntax

Following is the syntax to define a Perl format −

format FormatName =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
.

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 field holders. The field holders hold space for data that will be placed there at a later date. A field holder has the format −

@<<<<

This field holder 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 −

@>>>> right-justified
@|||| centered
@####.## numeric field holder
@* multiline field holder

An example format would be −

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.

In this example, $name would be written as left justify within 22 character spaces and after that age will be written in two spaces.

raja
Published on 29-Nov-2019 10:01:08
Advertisements