next up previous contents
Next: Format Statements Up: Practical Perl Programming Previous: Example: Using the Split()

Reports

Perl has a few special features that let you create simple reports. The reports can have a header area where you can place a title, page number, and other information that stays the same from one page to the next. Perl will track how many lines have been used in the report and automatically generate new pages as needed.

Compared to learning about regular expressions, learning how to create reports will be a breeze. There are only a few tricky parts

Let's start out by using the print() function to display a CD collection and then gradually moves from displaying the data to a fully formatted report.

The data file shown below is used for all of the examples in this chapter. The format is pretty simple:

The file is as follows:

Love Supreme!
A Kind of Blue!Miles Davis!9.99
Koln Concert!Keith Jarrett!15.99
Birds of Fire!Mahavishnu Orchestra!10.99

You'll find that Perl is very handy for small text-based data files like this. You can create them in any editor and use any field delimiter you like.

Now that we have some data, let's look at a program (report1.pl) that reads the data file and displays the information:

open(FILE, "<format.dat");
@lines = <FILE>;
close(FILE);

foreach (@lines) {
    chop;
    ($album, $artist, $price) = (split(/!/));
    print("Album=$album   Artist=$artist   Price=$price\n");
}

This program displays:

Use of uninitialized value at report1.pl line 8.
Album = Love Supreme Artist = Price =
Album = A Kind of Blue Artist =  Miles Davis Price = 9.99
Album = Koln Concert Artist = Keith Jarrett Price = 15.99
Album = Birds of Fire Artist = Mahavishnu Orchestra Price = 10.99

Why is an error being displayed on the first line of the output? If you said that the split() function was returning the undefined value when there was no matching field in the input file, you were correct. The first input line was the following:

A Love Supreme!

There are no entries for the Artist or Price fields. Therefore, the $artist and $price variables were assigned the undefined value, which resulted in Perl complaining about uninitialized values. You can avoid this problem by assigning the empty string to any variable that has the undefined value. To do this report2.pl:

open(FILE, "<format.dat");

@lines = <FILE>;

close(FILE);



foreach (@lines) {

    chop;

    ($album, $artist, $price) = (split(/!/));

    $album  = "" if !defined($album);  These lines assign null

    $artist = "" if !defined($artist); strings if no info is

    $price  = "" if !defined($price);  present in the record.

    print("Album=$album   Artist=$artist   Price=$price\n");

}

The first four lines this program displays are the following:

Album=A Love Supreme   Artist=                      Price=
Album=A Kind of Blue   Artist=Miles Davis           Price=9.99
Album=Koln Concert     Artist=Keith Jarrett         Price=15.99
Album=Birds of Fire    Artist=Mahvishnu Orchestra   Price=10.99

The error has been eliminated, but it is still very hard to read the output because the columns are not aligned. The rest of this chapter is devoted to turning this jumbled output into a report.

Perl reports have heading and have detail lines. A heading is used to identify the report title, the page number, the date, and any other information that needs to appear at the top of each page. Detail lines are used to show information about each record in the report. In the data file being used for the examples in this chapter , each CD has its own detail line.

Headings and detail lines are defined by using format statements, which are discussed in the next section.



 
next up previous contents
Next: Format Statements Up: Practical Perl Programming Previous: Example: Using the Split()
dave@cs.cf.ac.uk