Learning Perl

Learning PerlSearch this book
Previous: 10.6 The stat and lstat FunctionsChapter 10
Filehandles and File Tests
Next: 11. Formats
 

10.7 Exercises

See Appendix A, Exercise Answers for answers.

  1. Write a program to read in a filename from STDIN, 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 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/or executable, and which ones don't exist. (You can perform each test for each filename as you read them, or on the entire set of names when you've read them all. Don't forget to remove the newline at the end of each filename you have read in.)

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


Previous: 10.6 The stat and lstat FunctionsLearning PerlNext: 11. Formats
10.6 The stat and lstat FunctionsBook Index11. Formats