Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 10.7 The stat FunctionChapter 10
Filehandles and File Tests
Next: 11. Formats
 

10.8 Exercises

See Appendix A, Exercise Answers for answers.

  1. Write a program to read in a filename from STDIN, and then open that file and display its contents with each line preceded by the filename and a colon. For example, if fred was read in, and the file fred consisted of the three lines aaa, bbb, and ccc, you would see fred: aaa, fred: bbb, and fred: ccc.

  2. Write a program that prompts for an input filename, an output filename, a search pattern, and a replacement string, and then replaces all occurrences of the search pattern with the replacement string while copying the input file to the output file. Try it on some files. Can you overwrite an existing file? (Don't try it with anything important!) Can you use regular expression characters in the search string? Can you use $1 in the replacement string?

  3. Write a program to read in a list of filenames and then display which of the files are readable, writable, and nonexistent. (You can perform each test for each filename as you read them, or you can perform the tests on the entire set of filenames after you've read them all. Don't forget to remove the newline at the end of each filename that you have read.)

  4. Write a program to read in a list of filenames and then find the oldest file among them. Print out the name of the file and the age of that file in days.


Previous: 10.7 The stat FunctionLearning Perl on Win32 SystemsNext: 11. Formats
10.7 The stat FunctionBook Index11. Formats