Printing Revisited

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

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 a,, this:

open(OUTPUT_file,">testfile.dat");
$oldHandle=select(OUTPUT_FILE);
#select returns the file handle it is replacing
print("This is line1.\n");
select($oldHandle);
# restore the old file handle
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.

Format Specifiers for the printf() Function

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.
Format Modifiers for the printf() Function

# - Forces octal numbers to be printed with a leading zero. Hexadecimal numbers will be printed with a leading Ox. "A
+ - Forces signed numbers to be printed with a leading + or sign.
0 - Pads the displayed number with zeros instead of spaces.

Forces the value to be at least a certain width. For example, %10.3f 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. print("This is line I.In"); select($oldHandle); # restore the old file handle 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. Format Specifiers for the printf() Function Specifier Description Indicates that a single character should be printed. Indicates that a string should be printed. Indicates that a decimal number should be printed. Indicates that an unsigned decimal number should be printed. Indicates that a hexadecimal number should be printed. Indicates that an octal number should be printed. Indicates that a floating point number should be printed in scientific notation. Indicates that a floating point number should be printed. Indicates that a floating point number should be printed using the most space-spacing format, either e or f. Format Modifiers for the printf() Function Modifier Description - Indicates that the value should be printed left-justified. Forces octal numbers to be printed with a leading zero.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.