We've been using the print() function throughout this book without really looking at how it works. Let's remedy that now.
The print() function is used to send output to a file handle. Most of the time, we've been using STDOUT as the file handle. Because STDOUT is the default, we did not need to specify it. The syntax for the print() function is: print FILE_HANDLE (LIST)
You can see from the syntax that print() is a list operator because it's looking for a list of values to print. If you don't specify a list, then $ will be used. You can change the default file handle by using the select() function. Let's take a look at this:
open(OUTPUT_FILE, ">testfile.dat"); $oldHandle = select(OUTPUT_FILE); print("This is line 1.\n"); select($oldHandle); print("This is line 2.\n");
This program displays:
This is line 2.
and creates the TESTFILE.DAT file with a single line in it:
This is line 1.
Perl also has the printf() function which lets you be more precise in how things are printed out. The syntax for printf() looks like this:
printf FILE_HANDLE (FORMAT_STRING, LIST)
Like print(), the default file handle is STDOUT. The FORMAT_STRING parameter controls what is printed and how it looks. For simple cases, the formatting parameter looks identical to the list that is passed to printf(). For example:
$januaryCost = 123.34; $februaryCost = 23345.45; printf("January = \$$januaryCost\n"); printf("February = \$$februaryCost\n");
This program displays:
January = $123.34 February = $23345.45
In this example, only one parameter is passed to the printf() function-the formatting string. Because the formatting string is enclosed in double quotes, variable interpolation will take place just like for the print() function.
This display is not good enough for a report because the decimal points of the numbers do not line up. You can use the formatting specifiers shown below:
Specifier | Description |
---|---|
c | Indicates that a single character |
should be printed. | |
s | Indicates that a string should |
be printed. | |
d | Indicates that a decimal number |
should be printed. | |
u | Indicates that an unsigned decimal |
number should be printed. | |
x | Indicates that a hexadecimal number |
should be printed. | |
o | Indicates that an octal number |
should be printed. | |
e | Indicates that a floating point |
number should be printed | |
in scientific notation. | |
f | Indicates that a floating point number |
should be printed. | |
g | Indicates that a floating point number |
should be printed using | |
the most space-spacing format, either e or f. |
The formats can be modified as follows:
Modifier | Description |
---|---|
- | Indicates that the value should be printed left-justified. |
# | Forces octal numbers to be printed with a leading zero. |
Hexadecimal numbers will be printed with a leading 0x. | |
+ | Forces signed numbers to be printed with a leading + or - sign. |
Pads the displayed number with zeros instead of spaces. | |
. | Forces the value to be at least a certain width. |
An example use of . is: %10.3f
which means that the value will be at least 10 positions wide. And because f is used for floating point, at most 3 positions to the right of the decimal point will be displayed. %.10s will print a string at most 10 characters long.
Returning to our above example, to print the cost variables using format specifiers, we may write print.pl
$januaryCost = 123.34; $februaryCost = 23345.45; printf("January = \$%8.2f\n", $januaryCost); printf("February = \$%8.2f\n", $februaryCost);This program displays:
January = $ 123.34 February = $23345.45
This example uses the f format specifier to print a floating point number. The numbers are printed right next to the dollar sign because $februaryCost is 8 positions width.
If you did not know the width of the numbers that you need to print in advance, you could use the following technique:
In Perl we would do, printdemo.pl:
$januaryCost = 123.34; $februaryCost = 23345.45; $maxLength = length(max($januaryCost, $februaryCost)); printf("January = \$%$maxLength.2f\n", $januaryCost); printf("February = \$%$maxLength.2f\n", $februaryCost); sub max { my($max) = shift(@_); foreach $temp (@_) { $max = $temp if $temp > $max; } return($max); }
This program displays:
January = $ 123.34 February = $23345.45
While taking the time to find the longest number is more work, the result is worth it.