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. 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 a command line:

perl Moon

Some sample ...

Get Sams Teach Yourself Perl in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.