⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT><FONT FACE="Courier">format FORMAT_NAME =<BR>

Presentation Line&nbsp;&nbsp;#1<BR>

Values for&nbsp;&nbsp;Presentation Line&nbsp;&nbsp;#1<BR>

Presentation Line&nbsp;&nbsp;#2<BR>

Values for&nbsp;&nbsp;Presentation Line&nbsp;&nbsp;#2<BR>

Presentation Line&nbsp;&nbsp;#3<BR>

Values for&nbsp;&nbsp;Presentation Line&nbsp;&nbsp;#3<BR>

...</FONT></TT>

</BLOCKQUOTE>

<P>

The<TT><FONT FACE="Courier"> FORMAT_NAME</FONT></TT><I> </I>is

the name of the format to use. Any Perl variable name can be used

here to name a format, but the convention is to use all capital

letters in the name. The period at the end is required to signify

the end of a format specification. Formats work with file handles.

Each file handle in a Perl program can have a format defined for

it. The name of a format is the same as that of its associated

file handle. The default output file handle for a Perl script

is <TT><FONT FACE="Courier">STDOUT</FONT></TT>; therefore, the

format name for standard output is also <TT><FONT FACE="Courier">STDOUT</FONT></TT>.

For <TT><FONT FACE="Courier">stderr</FONT></TT>, the error output,

the format is <TT><FONT FACE="Courier">STDERR</FONT></TT>. In

the program shown in Listing 19.2 it is defined in lines 13 through

26.

<P>

Within the format definition, two lines of code are used for each

line that is printed as output. The first line of code indicates

how fields are to be displayed using placeholders, the second

describes the variables whose values are used to display in the

placeholders. The program in Listing 19.2 used two variables,

<TT><FONT FACE="Courier">$name</FONT></TT> and <TT><FONT FACE="Courier">$address</FONT></TT>,

to be printed out in two lines. See lines 14 through 17 in the

source code. Line 14 defines where to put the value of the next

variable, and line 15 defines <TT><FONT FACE="Courier">$name</FONT></TT>

to be the name of the variable whose value is used with the format

definition in line 13. Line 16 defines another placeholder, and

line 17 defines the variable to use to fill the placeholder specified

in line 16.

<P>

By using the placeholders in a format statement, you can carefully

place values of variables in specific locations in a series of

lines in an output. Placeholders can left, center, or right justify

the text, clip the output text, and so on to further beautify

the text.

<H2><A NAME="UsingtheformatStatement"><FONT SIZE=5 COLOR=#FF0000>Using

the </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">format</FONT></TT><FONT SIZE=5 COLOR=#FF0000>

Statement</FONT></A></H2>

<P>

Let's look at another sample use of this format for printing out

data. The data is on chemicals and is in a text file. The file

contains a list of chemicals in common household products. The

first script is for printing out this data in a nice, presentable

fashion.

<P>

Here is the sample data file:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Acetic Acid: Vinegar<BR>

Ammonium Hydroxide: Ammonia Cleaners<BR>

Ammonium Nitrate: Salt Peter<BR>

Ammonium Oleate: Ammonia Soap<BR>

Barium Sulfide: Black Ash<BR>

Carbon Carbinate: Chalk<BR>

Carbontetrachloride: Cleaning Fluid<BR>

Calcium Oxide: Lime<BR>

Ferric Oxide: Rust<BR>

Glucose: Corn Syrup<BR>

Graphite: Pencil Lead<BR>

Hydrogen Peroxide: Peroxide<BR>

Naphthalene: Mothballs<BR>

Silver Nitrate:&nbsp;&nbsp;Photographer's Hypo<BR>

Sodium Bicarbonate: Baking Soda<BR>

Sodium Borate: Borax<BR>

Sodium Carbonate: Washing Liquids<BR>

Sodium Chloride: Salt<BR>

Sodium Silicate: Glass<BR>

Sulfuric Acid: Battery Acid<BR>

Sucrose: Cane Sugar</FONT></TT>

</BLOCKQUOTE>

<P>

To print this file in a clean fashion, the obvious choice is to

use <TT><FONT FACE="Courier">printf</FONT></TT> statements with

a header and one <TT><FONT FACE="Courier">printf</FONT></TT> statement

per line in the file, as shown in the following fragment of code.

The output from this run is too garbled to print in a book.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &quot;\n %20s | %20s \n&quot; ,

&quot;Chemical&quot;, &quot; Product &quot;;<BR>

while(&lt;&gt;) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;my ($chemical, $found) = split(':');<BR>

&nbsp;&nbsp;&nbsp;&nbsp;printf &quot; %20s | %20s \n&quot;, $chemical,

$found;<BR>

&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></TT>

</BLOCKQUOTE>

<P>

Also note that you don't know what the data looks like. What if

you wanted to get an idea what the output should look like using

<TT><FONT FACE="Courier">format</FONT></TT> statements? The code

in Listing 19.3 illustrates how to print the file to get the following

output:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Chemical&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product

<BR>

=====================================================<BR>

Acetic Acid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vinegar

<BR>

Ammonium Hydroxide&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ammonia

Cleaners<BR>

Ammonium Nitrate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Salt

Peter<BR>

Ammonium Oleate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ammonia

Soap<BR>

Barium Sulfide&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Black

Ash<BR>

Carbon Carbinate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chalk

<BR>

Carbontetrachloride&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cleaning

Fluid<BR>

Calcium Oxide&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lime

<BR>

Ferric Oxide&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rust

<BR>

Glucose&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Corn

Syrup<BR>

Graphite&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pencil

Lead<BR>

Hydrogen Peroxide&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Peroxide

<BR>

Naphthalene&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mothballs

<BR>

Silver Nitrate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Photographer's

Hypo<BR>

Sodium Bicarbonate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Baking

Soda<BR>

Sodium Borate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Borax

<BR>

Sodium Carbonate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Washing

Liquids<BR>

Sodium Chloride&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Salt

<BR>

Sodium Silicate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Glass

<BR>

Sulfuric Acid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Battery

Acid<BR>

Sucrose&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cane

Sugar</FONT></TT>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 19.3. A simple report generator.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 while(&lt;&gt;) {<BR>

&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp; ($chemical, $found) = split(':');

<BR>

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp; write;<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

&nbsp;6 format STDOUT_TOP =<BR>

&nbsp;7 Chemical&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product

<BR>

&nbsp;8 =====================================================

<BR>

&nbsp;9 .<BR>

10 format STDOUT =<BR>

11 @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&nbsp;&nbsp;&nbsp;&nbsp;

@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

<BR>

12 $chemical, $found<BR>

13 .</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Line 4 is where each record is printed out with the use of the

<TT><FONT FACE="Courier">write()</FONT></TT> function. The <TT><FONT FACE="Courier">write()</FONT></TT>

function can take a <TT><FONT FACE="Courier">FILEHANDLE</FONT></TT>

parameter. Therefore, the following two lines are equivalent because

the default file handle is <TT><FONT FACE="Courier">STDOUT</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">write;write STDOUT;</FONT></TT>

</BLOCKQUOTE>

<P>

There is no guarantee, however, that you'll be writing to <TT><FONT FACE="Courier">STDOUT</FONT></TT>.

You should check to see which file handle you are writing to in

order to make sure you are using the correct format. I cover selection

of formats shortly.

<P>

In the format specifications on lines 6 through 9, the name of

the format is <TT><FONT FACE="Courier">STDOUT_TOP</FONT></TT>.

<TT><FONT FACE="Courier">STDOUT</FONT></TT> is the name that corresponds

to the file handle you happen to be writing to, which in this

case is the standard output: <TT><FONT FACE="Courier">STDOUT</FONT></TT>.

The <TT><FONT FACE="Courier">_TOP</FONT></TT> appendage is for

the <TT><FONT FACE="Courier">write()</FONT></TT> statement to

print this header every time it starts a new page. The format

specification is terminated with the solitary period on line 9.

<P>

Perl actually makes the assumption that most output from reports

will be sent to a printer. Therefore, when the line count goes

to zero, Perl prints out the value in the<TT><FONT FACE="Courier">

$^</FONT></TT> variable for the top of page. If you want your

output to go to a screen, you might not want the top of page format

to be printed out. In such a case, set <TT><FONT FACE="Courier">$^

</FONT></TT>to <TT><FONT FACE="Courier">NULL</FONT></TT>, and

you will get continuous output. The default number of lines per

page is set to 60. The <TT><FONT FACE="Courier">$^</FONT></TT>

variable is set to a default format with a value of a form feed

to start a new page on a printer.

<P>

There are two points that should be mentioned here. One, the format

specification for the header or the record is not specified in

the <TT><FONT FACE="Courier">write()</FONT></TT> call. Two, the

header is printed only once automatically before the first record

is printed.

<P>

Lines 10 through 13 contain the format specifications to be used

for each record printed with the <TT><FONT FACE="Courier">write()</FONT></TT>

function in line 4. The format is specified in two lines for each

line of output. The first line specifies how to print the information,

and the second line specifies what variables are used to print

the information. As with the header, the format specification

is terminated with a solitary period (see line 13).

<P>

The format specifiers in Perl have an at symbol (<TT><FONT FACE="Courier">@</FONT></TT>)

followed by these symbols:

<UL>

<LI><FONT COLOR=#000000>The less than symbol </FONT>(<TT><FONT FACE="Courier">&lt;</FONT></TT>)

for left-justified text.

<LI><FONT COLOR=#000000>The </FONT>greater than symbol (<TT><FONT FACE="Courier">&gt;</FONT></TT>)

for right-justified text.

<LI><FONT COLOR=#000000>The </FONT>pipe symbol (<TT><FONT FACE="Courier">|</FONT></TT>)

for centered text.

<LI><FONT COLOR=#000000>The</FONT> asterisk (<TT><FONT FACE="Courier">*</FONT></TT>)

for an unlimited line of text.

<LI><FONT COLOR=#000000>The </FONT>hash mark (<TT><FONT FACE="Courier">#</FONT></TT>)

for a digit in numeric values.

<LI><FONT COLOR=#000000>The </FONT>period (<TT><FONT FACE="Courier">.</FONT></TT>)

between hash marks for a decimal point.

<LI><FONT COLOR=#000000>The </FONT>caret (<TT><FONT FACE="Courier">^</FONT></TT>)

instead of the at symbol (<TT><FONT FACE="Courier">@</FONT></TT>)

for splitting the contents of a text variable on multiple lines

through the use of padded lines. The value of the <TT><FONT FACE="Courier">@^</FONT></TT>

is modified.

<LI><FONT COLOR=#000000>The </FONT>tilde (<TT><FONT FACE="Courier">~</FONT></TT>)

for suppressing lines that are empty. The double tilde (<TT><FONT FACE="Courier">~~</FONT></TT>)

signifies a <TT><FONT FACE="Courier">^</FONT></TT> format that

is repeated on several lines to allow the contents of a variable

to be printed on several lines. When using the <TT><FONT FACE="Courier">~~</FONT></TT>,

the output pads fields into a format until there are no more characters

to print out.

</UL>

<P>

The symbols after <TT><FONT FACE="Courier">@</FONT></TT> are repeated

to signify the number of columns to take up on the output. For

example, to create a left-justified field that is 10 characters

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -