Formatting your Output

Perl enables you to produce formated output using print formats and the built-in function write.

Using Print Format

Defining format MYFORMAT =                           # start with the format keyword
===================================
Here is the text i want to display.
===================================
.                                           # terminated by a single dot
Display a print format

$~ = "MYFORMAT";                            # set the system variable $~ to the format you want to use
write;                                      # call the built-in write function

format MYFORMAT =                                 
===================================
Here is the text i want to display.
===================================
.                                           # don't forget the single dot

Displaying Valuesin a Print Format

$line = "hello this is a test program";

$line =~ s/[^aeiou]//g;
@vowels = split(//, $line);
foreach $vowel (@vowels)
{
   $vowelcount{$vowel} += 1;
}

$~ = "MYFORMAT";
write;

format MYFORMAT =
=====================================================
Number of vowels found in text file:
      a: @<<<<< e: @<<<<<
      $vowelcount{"a"}, $vowelcount{"e"}
      i: @<<<<< o: @<<<<<
      $vowelcount{"i"}, $vowelcount{"o"}
      u: @<<<<<
      $vowelcount{"u"}
=====================================================
.

Note: the @<<<<< means six left justified characters

There are a number of fields formats that can be used

Left-Justified output @<<<       # 4 left-justified characters ( (@=1 character) + (<<<=3 characters) ) = 4 characters total
@<<<<<     # 6 left-justified characters
Right-Justified output @>>>
@>>>>>
Centered output @||||      # 4 centered characters
Fixed-precision numeric @##.##     # 3 numbers before decimal point 2 numbers after example 345.78
Multiline text @*         # any length

There are other formating utilies

Specifying a Page Number

$~ = "PAGE_FOOTER";
write;

format PAGE_FOOTER =
page @<<<
$%
.

Note: the system variable $% holds the current page number and starts from 0

Set the page length $= = 66;             # system variable $= hold the page length
Change the Header Print Format $~ = "MYFORMAT";
$^ = "TOP_OF_PAGE";
write;

format MYFORMAT =
myformat
.
format TOP_OF_PAGE =
top of the page
.

Balance formatting and removing blank lines $string = ("this is a\n line of string to\n confuse outputting

added a couple of blank lines \n");

$~ = "MYFORMAT";
write;

format MYFORMAT =
~ ^>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$string
.

Note: the ^<< trys to fit as many lines as possible, @* will print as is including newlines
~ will remove any blank lines

Putting it all together

open (OUTFILE, ">file1");
select (OUTFILE);
$~ = "WRITELINE";
$^ = "TOP_OF_PAGE";
$= = 60;

while ($line = <STDIN>)
{
   write;
}

close (OUTFILE);

format TOP_OF_PAGE =
                    -Page @<
                          $%
.
format WRITELINE =
@>>>>>>>>>>>>>>>>>>>>
$line
.

Using printf

Perl borrows from the C language the printf function and is used just the same, the field specifiers are below

Single Character %c
Integer %d
Floating-point in scientific notation %e
Floating-point in normal fixed-point notation %f
Floating-point in compact format %g
Integer in octal %o
Character String %s
Unsigned integer %u
nteger in hex format %x
Examples

printf("The number is %d", $number);
printf("The house number is %d and the town is %s", $number, $town);

printf("amount: %20d", $salary);     # 20 fields of characters (right justified)
printf("amount: %-20d", $salary);    # 20 fields of characters (left justified)

printf("sale cost: %5.2f", $cost);   # 5 = total number of fields including the decimal point
                                     # 2 = total numbers after decimal point example:   99.99