Exercises for Chapter 6

§6.1
Write a Perl script that throws away any values for the DATA filehandle included at the end of another Perl script. (Use a line range.)
 6.2
Write a Perl script that prints the longest paragraph (measured in words) in a document.
[Hints/Solution for 6.2]
 6.3
Although you will often see it said that NTSC video has a frame rate of 30fps, this is not actually true: for obsolete technical reasons, NTSC video is broadcast at a rate of 29.97 frames per second. Since there is not an exact number of frames in a second, SMPTE timecode, which must use exactly 30, drifts with respect to the elapsed time. The expedient adopted to work round this is called drop frame timecode, in which frames 00:00 and 00:01 are omitted at the start of every minute except the tenth. (It's a bit like a leap year.) So your count jumps from, say, 00:59:29 to 1:00:02. Adapt the script on pages 133--134 to add extra columns for drop frame timecodes, changing the layout appropriately to arrange all the information neatly on the page.
*6.4
As you probably know, the GIF image file format can accommodate multiple images in a single file; this is the basis of GIF animation, which is currently enjoying a vogue on the World-Wide Web. Write a Perl script that examines a GIF file and finds out how many frames it contains. To do this, you will need to read the GIF format specification. It is available at ftp://ftp.ncsa.uiuc.edu/misc/file.formats/graphics.formats/gif89a.doc, and is also described in The Encyclopedia of Graphics File Formats, by James D. Murray and William vanRyper (O'Reilly and Associates, 2nd ed., 1996).
[Hints/Solution for 6.4]
 6.5
Adapt your answer to exercise 5.10 to produce a script that scans a directory and changes the names of all the files in it to conform to MS-DOS restrictions. Write a log file recording the changes in a suitable format for subsequently querying for the original name or reversing the renaming process.
 6.6
Consider the sequence of numbered pictures produced as the output of the script on page 144. Suppose that it turns out that you want to make a movie out of these pictures in reverse order (stranger things have happened). It is possible to reverse movies, but sometimes it is quicker just to reverse the sequence numbering of the individual pictures.
  1. Assuming that the pictures have consecutive three digit extensions, starting at .001, write a Perl script that reverses their order; i.e. if there are 48 pictures, they will be called something like frame.001 to frame.048; your script should rename the original frame.001 as frame.048, frame.002 as frame.047, and so on, until the original frame.048 becomes the new frame.001. (Take care not to... but I don't have to tell you that, do I?)
  2. Instead of assuming that the pictures have consecutive three digit extensions, starting at .001, add code to find out how long the extensions are and which is the starting number, and to verify that they are indeed consecutive. (What will you do if they are not?) Modify the reversal code so that it will correctly rename the files consistently with their original numbering scheme. (For example, if the original extensions ran from .07 to .45, so should the new ones, but with the pictures in reverse order.)

[Hints/Solution | Test data for 6.6]


Exercises for previous chapter | Exercises for next chapter | Perl: The Programmer's Companion