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

📄 ch19.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 19 -- Generating Reports</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 19</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Generating Reports</FONT></B></H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>



<UL>

<LI><A HREF="#FormattedOutput" >Formatted Output</A>

<LI><A HREF="#DefiningaFormat" >Defining a Format</A>

<LI><A HREF="#UsingtheformatStatement" >Using the format Statement</A>

<LI><A HREF="#UsingMoreThanOneFormat" >Using More Than One Format</A>

<LI><A HREF="#ControllingtheFormat" >Controlling the Format</A>

<LI><A HREF="#CreatingMultipleLineswiththeCaret" >Creating Multiple Lines with the Caret</A>

<LI><A HREF="#UsingformatinModules" >Using format in Modules</A>

<LI><A HREF="#AnotherExampleofReportGeneration" >Another Example of Report Generation</A>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

This chapter presents techniques for generating reports with Perl

using the built-in format specifiers. Perl stands for Practical

Extraction Report Language; this chapter covers how to extract

information in the form of reports using Perl. (The name of Perl

is also quoted as &quot;Pathologically Eclectic Rubbish Lister,&quot;

in Larry Wall's book, <I>Programming Perl</I>.)

<H2><A NAME="FormattedOutput"><FONT SIZE=5 COLOR=#FF0000>Formatted

Output</FONT></A></H2>

<P>

Perl has excellent report-generation capabilities. You have already

used the <TT><FONT FACE="Courier">print</FONT></TT> and <TT><FONT FACE="Courier">printf</FONT></TT>

statements to write out text. Perl also has the capability to

print out reports using formats. By using formats, you can actually

visualize how your output will look because the definition of

a format in Perl is very similar to what you see on the output.

<P>

There are three steps that you must take to use formats with Perl:

<OL>

<LI>Define the format and the variables that apply to the format.

<LI>Initialize the variables used in the format.

<LI>Output to the file handle of the format with the <TT><FONT FACE="Courier">write()</FONT></TT>

function.

</OL>

<P>

We cover these steps in detail throughout the rest of the chapter.

Let's start off with a quick example of how generating reports

works with a sample letter writing application. This example will

give you a quick overview of what's entailed in using formats.

After this example, we will cover specific details of how to use

the format specification.

<P>

Up to now, we have covered printing only with the use of <TT><FONT FACE="Courier">printf</FONT></TT>

or <TT><FONT FACE="Courier">print</FONT></TT> statements. It's

a bit difficult to see what you are actually printing out when

you read <TT><FONT FACE="Courier">print</FONT></TT> statements

and the ensuing double quotes around the variables being printed.

What would be nice is if you could lay out the approximate structure

of a page and basically insert placeholders for where you want

the output to go.

<P>

For example, let's say that you want to create a simple letter

that you want to mail to your customers whose names and addresses

are stored in a text file. You would first type in a generic letter

like the one shown here:

<BLOCKQUOTE>

<TT><I><FONT FACE="Courier">FIRSTNAME LASTNAME<BR>

ADDRESS<BR>

<BR>

</FONT></I><FONT FACE="Courier">Dear <I>FIRSTNAME:<BR>

</I>I am pleased to announce the new whizbang needle sharpening

tool.<BR>

Give me a call if you are interested.<BR>

<BR>

<BR>

Sincerely,<BR>

Ipik Freely.</FONT></TT>

</BLOCKQUOTE>

<P>

In the letter above, the fields <TT><I><FONT FACE="Courier">FIRSTNAME</FONT></I></TT>,

<TT><I><FONT FACE="Courier">LASTNAME</FONT></I></TT>, and <TT><I><FONT FACE="Courier">ADDRESS</FONT></I></TT>

are placeholders for the actual first name, last name, and complete

address of each individual who will receive the letter. Now that

you have your letter defined, you would write code, in Perl naturally,

to print one letter for each record in your database. Each printed

letter would have the <TT><I><FONT FACE="Courier">FIRSTNAME</FONT></I></TT>,

<TT><I><FONT FACE="Courier">LASTNAME</FONT></I></TT>, and <TT><I><FONT FACE="Courier">ADDRESS</FONT></I></TT>

placeholders replaced with what is in an input record.

<P>

Listing 19.1 is a sample application which generates a listing

from the letter shown above.

<HR>

<BLOCKQUOTE>

<B>Listing 19.1. A simple report generator without using formats.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2 open (NAMES,&quot;names&quot;) || die &quot;Cannot open

names $!&quot;;<BR>

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

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;($fname,$lname,@address) = split(':',$_);

<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n\n&quot;;<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$fname $lname \n&quot;;

<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$address \n\n&quot;;

<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;print &quot;I am very pleased to

announce the new TOOTHPX3000 now with a\n&quot;;<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;print &quot;rechargeable battery

and direct 110-220 Volt adapter. Give me\n&quot;;<BR>

10&nbsp;&nbsp;&nbsp;&nbsp;print &quot;a call for a demo.\n&quot;

;<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n&quot;;<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Sincerely, \n&quot;;<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Ipik Freely,\n&quot;;<BR>

14&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Prezdet.\n\f&quot;;<BR>

15}<BR>

16 close NAMES;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The data file for this is simple and is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Big:Wolf:1 Tree Lane<BR>

Wise:Pig :333 Brick house<BR>

NotSoWise:Pig:666 Straw House</FONT></TT>

</BLOCKQUOTE>

<P>

There is one record per line in the file. Each field in every

record is delimited by a colon (:) just like fields in the <TT><FONT FACE="Courier">/etc/passwd</FONT></TT><I>

</I>file. The first field in the line is the first name; the second

field is the last name; and the rest of the line is the address

of the individual. This is a contrived example for this chapter,

so your own database would be different. For example, the address

could be split into street address, city, and state. Or you could

have a phone number as the third field. For this example, let's

take the first name, last name, and address values into three

variables, <TT><FONT FACE="Courier">$fname</FONT></TT>, <TT><FONT FACE="Courier">$lname</FONT></TT>,

and <TT><FONT FACE="Courier">$address</FONT></TT> at line 4 in

Listing 19.1. Then you will print out these lines. The output

is as follows (with the page breaks called out as such):

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Big Wolf<BR>

1 Tree Lane<BR>

<BR>

<BR>

I am very pleased to announce the new TOOTHPX3000 now with a<BR>

rechargeable battery and direct 110-220 Volt adapter. Give me

<BR>

a call for a demo.<BR>

<BR>

Sincerely,<BR>

Ipik Freely,<BR>

Prezdet.<BR>

--page break--<BR>

<BR>

<BR>

Wise Pig<BR>

333 Brick house<BR>

<BR>

<BR>

I am very pleased to announce the new TOOTHPX3000 now with a rechargeable

battery and direct 110-220 Volt adapter. Give me a call for a

demo.<BR>

<BR>

Sincerely,<BR>

Ipik Freely,<BR>

Prezdet.<BR>

--page break--<BR>

<BR>

<BR>

NotSoWise Pig<BR>

666 Straw house<BR>

<BR>

<BR>

I am very pleased to announce the new TOOTHPX3000 now with a rechargeable

battery and direct 110-220 Volt adapter. Give me a call for a

demo.<BR>

<BR>

Sincerely,<BR>

Ipik Freely,<BR>

Prezdet.<BR>

--page break--</FONT></TT>

</BLOCKQUOTE>

<P>

In Listing 19.1, the lines of code to print the output are cumbersome

to look at. Note how the final form of the letter to print out

is not immediately apparent from examining the code in all the

print statements. Also, typing the print statements is prone to

error while typing. Let's see how the three steps to define and

use a format could be applied here to print a more human-readable

Perl program, as shown in Listing 19.2.

<HR>

<BLOCKQUOTE>

<B>Listing 19.2. A simple report generator using formats.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2<BR>

&nbsp;3 open (NAMES,&quot;names&quot;) || die &quot;Cannot open

names $!&quot;;<BR>

&nbsp;4<BR>

&nbsp;5 $~ = NAME_FORMAT;<BR>

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

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp; ($fname,$lname,$address) = split('

',$_);<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp; $name = $fname . &quot; &quot;

. $lname;<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp; write;<BR>

10&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

11 close NAMES;<BR>

12<BR>

13 format NAME_FORMAT =<BR>

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

<BR>

15 $name<BR>

16 @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&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>

17 $address<BR>

18<BR>

19 I am very pleased to announce the new TOOTHPX3000 now with

a<BR>

20 rechargeable battery and direct 110-220 Volt adapter. Give

me<BR>

21 a call for a demo.<BR>

22<BR>

23 Sincerely,<BR>

24 Ipik Freely,<BR>

25 Prezdet.<BR>

26 .</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The input file is opened in line 3. Each record is split into

the first name, last name, and the address fields in line 7. A

full name is created in line 8. The entry is written to <TT><FONT FACE="Courier">STDOUT</FONT></TT>

in line 9. The input file is closed in line 11. Lines 13 to 26

define the format with the placeholders for the variables.

<H2><A NAME="DefiningaFormat"><FONT SIZE=5 COLOR=#FF0000>Defining

a Format</FONT></A></H2>

<P>

Formats can be defined anywhere in the code for a program. Here's

the syntax for a format specification:

<BLOCKQUOTE>

⌨️ 快捷键说明

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