1.17 Exercises
1. (Easy: System Exploration, Software Installation)
Check if you have Perl on your system. Learn to download Perl from the Internet if you do not have it. Perl comes pre-loaded on most Unix systems. If you do not have Perl, go to www.cpan.org and find Perl for your system. If you are on a Windows system, www.activestate.comÕs ActivePerl is the best bet. If you are using a pre-OS X Macintosh, download and install MacPerl from www.macperl.com. Write down the exact location where Perl is loaded on your system. Also, write down where all the Perl modules are saved.
2. (Easy: Web Surfing, Documentation Reading)
Learn to navigate www.cpan.org, The Comprehensive Perl Archive Network. Learn how to search for documentation on core Perl to know more about built-in variables and functions, and other information about so-called core Perl, i.e., the language itself without any modules. Also, learn to search module documentation.
3. (Easy: System Exploration, Documentation Reading)
Learn to use perldoc to find documentation on Perl. perldoc should work if the Perl system has been installed properly. Examples of usage are given below.
%perldoc -f my
%perldoc -q quote
%perldoc CGI
The first uses the -f option to obtain information on a Perl function. The second uses the -q option to obtain information on a keyword. The third obtains information on Perl module.
4. (Easy: System Exploration; Module Installation)
Learn to install a Perl module. On a Unix system, learn to use
%perl -MCPAN -e shell
to search for installed modules, and modules available at www.cpan.org or many of its mirrors. On a Windows machine, if you installed ActivePerl from www.activestate.com, you should be able to use the command called ppm to do the same. ppm stands for Perl Package Manager.
5. (Easy: Drill)
Type in each of the programs in this chapter on your computer and see if it works. The programs as given in this chapter work on a Unix machine. If you have a PC or a Macintosh, find out what modifications, if any you need to make to make the programs work.
6. (Easy: File Access)
Write two Perl scripts that do the following:
(a) Write a program that keeps an integer in a file that you name. Every time you call the script, the counter is incremented by 1. This is a counter. The counter does not have a name.
(b) Now, write a script that keeps track of as many counters as needed. The values are all kept in the same file. Each one is incremented by one when you call the script with the appropriate counter name as a command-line argument. Each counter has a name. A line of the file has the counter name followed by two colons and then the count. Assume the counter name has no space in it.
7. (Medium: Regular Expressions, Pattern Matching)
Write a program that removes all comments from a Perl program. Given a program file, it creates a new file where the comments have been removed. It checks to see if the first line has a comment starting at column 1. It does not remove this comment. A comment starts with #. In general, a comment can start at any column. A comment starting with # continues till the end of the current line. The program name is given as a command-line argument. The modified program has the same name as the original, but is written to a different directory.
Extend the program so that it removes comments from every Perl script file in a directory. Assume the name of a Perl script file ends in .pl or .pm. The program looks only at Perl scripts. The modified program have the same name as the original, and are written to a different directory that has been created earlier.
8. (Medium: Regular Expressions, File Handling)
Write a program that takes a command-line argument that is a file name. It asks for the location of the Perl interpreter on your computer. It then looks at the file name, and if the name ends in .pl, checks to see if the first line is a comment specifying the location of the Perl interpreter. If not, it adds the comment as seen in programs in this book. The first comment line is not needed on PCs and older Macintosh operating systems. However, it is not a bad idea to place them if we want the programs to be portable across systems. Remember paths on PCs and older Macintoshes are written differently than on a Unix machine.
Extend the program so that the comment line specifying the location of the interpreter is added to every file in a directory, if necessary.
9. (Easy: Arithmetic)
Write a program that asks for someoneÕs height in feet and inches, and converts it into meters. It writes the result in the form x.xxx where x before the decimal point is the meter value and the three xs after the decimal point are centimeters.
10. (Easy: Arithmetic)
Write a program that takes four numbers as command-line arguments. The first two give a distance in feet and inches to start from. The last two give a distance value to end. The program converts distances in increments of one inch from feet and inches to meters and centimeters. It prints the results in a nice-looking tabular format to the screen, and to a file.
Write versions of the program using for and while.
11. (Easy: Subroutine, Arithmetic)
Write a subroutine that converts a length given in feet and inches into centimeter. Use this subroutine to write a modification of the previous program.
12. (Medium to Hard: File Handling, Statistics, Graphics)
Write a program that prints the number of lines in a file whose name is given as command-line argument.
Extend the program so that it prints the name of every file in a directory and the number of lines in it. It prints the result to a file.
Extend the program further so that the results are are printed in the form a horizontal histogram. Scale the numbers so that the histogram lines fit the screen. Usee repetition of an alphabetic character, say x, to draw the lines.
13. (Medium: File Handling, Statistics, Arrays)
Write a program that obtains the names of all files in a directory. It stores the names in an array. The program also has a second array. In the second array, it stores the number of lines in each of the files. The two arrays have 1-1 mapping. Finally, the program prints, in a nice tabular form, the name of the file and the number of lines in it. Print the result to a file.
14. (Medium: File Handling, Statistics, Hashes)
Write a program that obtains the names of all files in a directory. This time, the program contains a hash. The keys in the hash are names of files in the directory. The values are the line counts. The program fills up this hash, and then prints the names of files and corresponding line counts in a nice tabular format to a file. It also summarizes with the total number of files in the directory, and the total space taken by all the files.
15. (Easy: Command-line Arguments)
Write a program that takes a few command-line arguments. Some of the command-line arguments have the non-alphanumeric character @ in them. It prints out all command-line arguments that contain @ and ignores the others. It prints the output one per line.
16. (Easy: File Test)
Write a program that prints out the names of all text files in a directory. The directory name is given as command-line argument.
17. (Easy: File Test, Loop)
Write a program that looks at all files in the current directory. First, it prints a list of all the text files. Next, it prints a list of all the binary files. It prints a nice heading to each of the two sets of files. If there are no files in a certain set, it does not print the corresponding heading. Do not obtain the names of all files in a directory twice.
18. (Easy: Debugger, System Exploration, Documentation Reading)
Learn how to use the debugger in Perl. Run all the programs given above through the debugger.
