Previous Page Next Page

10.5. What's Next?

Until this point, all the functions you have used were provided by Perl. The print() and printf(), push(), pop(), chomp() functions are all examples of built-in functions. All you had to know was what they were supposed to do and how to use them. You did not have to know what the Perl authors did to make the function work; you just assumed they knew what they were doing. In the next chapter, you will write your own functions, also called subroutines, and learn how to send messages to them and return some result.

Exercise 10: Getting a Handle on Things

Exercise A

1.Create a filehandle for reading from the datebook file (on the CD); print to another filehandle the names of all those who have a salary greater than $50,000.
2.Ask the user to input data for a new entry in the datebook file. (The name, phone, address, etc., will be stored in separate scalars.) Append the newline to the datebook file by using a user-defined filehandle.

Exercise B

1.Sort the datebook file by names, using a pipe.
2.Create a filehandle with the open function that uses a pipe to list all the files in your current directory and will print only those files that are readable text files. Use the die function to quit if the open fails.
3.Rewrite the program to test if any of the files listed have been modified in the last 12 hours. Print the names of those files.

Exercise C

1.Create a number of duplicate entries in the datebook file. Fred Fardbarkle, for example, might appear five times, and Igor Chevsky three times, etc. In most editors, this will be a simple copy/paste operation.

Write a program that will assign the name of the datebook file to a scalar and check to see if the file exists. If it does exist, the program will check to see if the file is readable and writeable. Use the die function to send any errors to the screen. Also tell the user when the datebook was last modified.

The program will read each line of the datebook file giving each person a 10% raise in salary. If, however, the person appears more than once in the file (assume having the same first and last name means it is a duplicate), he will be given a raise the first time, but if he appears again, he will be skipped over. Send each line of output to a file called raise. The raise file should not contain any person's name more than once. It will also reflect the 10% increase in pay. Display on the screen the average salary for all the people in the datebook file. For duplicate entries, print the names of those who appeared in the file more than once and how many times each appeared.

2.Write a script called checking that will take any number of filenames as command-line arguments and will print the names of those files that are readable and writeable text files. The program will print an error message if there are no arguments, and exit.


Previous Page Next Page