An Example: Creating Links

Here's a useful example I tend to use a lot in real life: Given a directory full of files that are either GIF or JPEG images (that is, they have .gif, .jpeg, or .jpg extensions), this script will generate an HTML file, called index.html, which contains nothing but links to each of the files. This is a good way to get a whole lot of images up on the Web really quickly without having to spend a lot of time creating HTML files (or it at least provides a basic file you can then edit to make it read better).

The script I wrote for this task uses file handles to open and write to the index.html file, directory handles to read the contents of the current directory, and file tests and regular expressions to match the files we're actually looking for. Listing 17.1 shows the result.

Listing 17.1. The imagegen.pl Script
1:  #!/usr/ bin/perl -w
2:  use strict;
3:  use Cwd;
4:
5:  open(OUT,">index.html") or die "Can't open index file: $!
";
6:  &printhead();
7:  &processfiles();
8:  &printtail();
9:
10: sub printhead {
11:     my $curr = cwd();
12:     print OUT "<html>
<head>
";
13:     print OUT "<title>Image files in directory $curr</title>
";
14:     print OUT "</head>
<body>
";
15:     print OUT "<h1>Image Files</h1>
";
16:     print OUT "<p>";
17: }
18:
19: sub processfiles {
20:     opendir(CURRDIR, '.') or die "Can't open directory ($!), exiting.
";
21:     my $file = "";
22:
23:     while (defined($file = readdir(CURRDIR))) {
24:         if (-f $file and $file =~ /(.gif|.jpe?g)$/) {
25:             print OUT  "<a href="$file">$file</a><br />
";
26:         }
27:     }
28:     closedir(CURRDIR)
29: }
30:
31: sub printtail {
32:     print OUT "</p></body></html>
";
33:     close(OUT);
34: } sub printtail {
    print OUT "</p></body></html>
";
    close(OUT);
}

We start by opening the output file (index.html) in line 5. Note the > character, which indicates an output file handle.

The &printhead() subroutine, in lines 10 through 17, simply prints out the top part of an HTML file. The only tricky part is the use of the current working directory in the title of the page, which we get using the cwd() function (as imported from the Cwd module in line 3). I didn't bother using a “here” document to print out the HTML code; that would have ended up being even more lines than are already here.

The &processfiles() subroutine (lines 19 through 29) is where the real work goes on. Here, we open the current directory in line 20, and then loop over each filename in that directory in the while loop in lines 23 through 26. The test in line 24 checks to see if the file is an actual file and not a directory (the -f test), and uses a regular expression to only operate on image files (those that have a .gif, .jpg, or .jpeg extension). If the current file is indeed an image file, we print out a link to that file in line 25, using the filename as the actual link text.

Note that using the filename as the link text doesn't make for very descriptive linksyou could end up with a set of links that say “image1.gif,” “image2.gif,” “image3.gif,” and so on. Not very descriptive, but it's a start. After the script runs you could edit the HTML to create more descriptive links (“nice sunset,” “Easter parade,” “Bill behaving foolishly,” and so on).

To finish up the script we call the &printtail() subroutine, which simply prints the end of the HTML file and closes the output file handle.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset