Create a Report Header using Perl


Sometime 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 using Perl. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable −

Example

 Live Demo

#!/usr/bin/perl

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

format EMPLOYEE_TOP =
===================================
Name Age
===================================
.

select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;

@n = ("Ali", "Raza", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);

$i = 0;
foreach (@n) {
   $name = $_;
   $age = $a[$i];
   $salary = $s[$i++];
   write;
}

Output

Now your report will look like −

===================================
Name                   Age
===================================
===================================
Ali                    20
2000.00
===================================
===================================
Raza                  30
2500.00
===================================
===================================
Jaffer                40
4000.00
===================================

Updated on: 29-Nov-2019

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements