File input / output

The printf function

Use the printf function to format your program output, e.g. to specify the number of decimal digits of floating point numbers or to emulate a tabular representation by inserting spaces between the printed values.

The general structure of the printf function is:

printf FILEHANDLE FORMAT, LIST

FORMAT contains a format control string, and LIST contains a list of variables that will be printed according to the format specified in FORMAT.

Read more about the printf and sprintf functions in the Programming Perl book, or briefly in the Learning Perl book.

For example, to print $a and $b so that $a is printed in a 24 character field aligned to the left and $b is printed as a floating point value with 2 decimal places and a minimum total length of 5 characters, write:
printf FILEHANDLE "%-24s %5.2f\n", $a, $b;

An example is also available in the Arrays assignment.


Table of Contents.