Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

What are formats in Perl?

Muhammad Ashir

Formats in Perl are used to display output in a specified format. They help you create reports and charts easily by creating a format for how the data is to be presented. They can be used to describe the number of lines on a page, what to print in the page header, what page you are on, etc.

Formats have their own namespace, so a function and a format may have the same name. However, the general naming convention is to give the format of a specific file handle the same name as the file handle.

How to declare a format

format Name = 
Format List
.

The name of the format is usually the same as the output file handle it is targeting. However, if the name is omitted, it is set to STDOUT by default.

The Format List is a series of lines, each of which is one of the following types:

  • A picture line
  • A comment
  • An argument line supplying arguments to the previous picture line

Picture line

A picture line consists of a bunch of string literals intermingled with fieldholders. A fieldholder is a character or text that is used as a placeholder for data that will be inserted in that spot later on.

fieldholder

description

@

start of regular field

^

start of special field

<

pad character to left justify

|

pad character for center alignment

>

pad character for right justify

#

padding for right-justified numeric field

0

inserts leading zeros instead of #

.

decimal point

...

truncate text and show "..." where truncated

@*

variable width field for multi-line value

^*

variable width field for next line of a multi-line value

~

suppress line with all fields empty

~~

repeat line until all fields are exhausted

Invoking format

To use format, we need the write keyword. Since the format will send the output to the file handle with the same name, we need to redirect it to STDOUT if we want the format to print to the standard output.

We can do that by using the select function to select the appropriate file handle and then using the special variable $~ to store the format name:

format Test_format = 
# format lines will go here
.
select(STDOUT)
$~ = Test_format
write

Code

format MyAccount =
============================
ACCOUNT TITLE: @<<<<<<<<<<<< 
$title
ACCOUNT NO: @0##########
$number
IBAN: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$iban
============================
.

@names = ("Ali", "Alice", "Bob");
@accounts = (123124235123, 894353459203, 234554235);
@ibans = ("NL56ABNA2238591354", "PK30DXOA7585964683852856", "IT90W0300203280575122754831");

select(STDOUT);
$~ = MyAccount;

$j = 0;
foreach (@names){
    $title = $_;
    $number = $accounts[$j];
    $iban = $ibans[$j];
    $j++;
    write;
} 

Here, we declare a format called MyAccount. It prints the account title, account number, and IBAN in the specified format.

The @< specifies that the text needs to be left-aligned. The @0########## specifies a numeric-field that will pad with leading zeros.

We can use a foreach loop to iterate through the lists of data and then assign them to the variables inside the format. The call to write will then print the formatted data on STDOUT.

RELATED TAGS

perl

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring