Exercise: Cleaning Up Input Data

The “blind” substitutions in the preceding example—where substitutions are made, but the exit status isn't checked—are common when you're trying to cook data. Cooking data is taking data from a user or a file that is not formatted exactly the way you would like and reformatting it. Listing 6.1 shows a routine to convert your weight on the earth to your weight on the moon, which demonstrates data manipulation.

Using your text editor, type the program in Listing 6.2 and save it as Moon. Of course, don't type the line numbers. Be sure to make the program executable according to the instructions you learned in Hour 1, “Getting Started with Perl.”

When you're done, try running the program by typing the following at ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.