Previous Page Next Page

11.5. What's Next?

In the next chapter, you will expand your horizons and go from the "introverted" Perl programmer to the "extroverted" programmer. Instead of writing stand-alone scripts, you will start learning how to use the libraries and modules already provided by Perl. You will explore CPAN and learn how to download and use modules that have been written my other programmers.

You will understand packages and namespaces and how to export and import symbols how to use the standard Perl library, and how to create your own. You will learn how to create procedural modules and how to store and use them.

Exercise 11: I Can't Seem to Function without Subroutines

1.Write a program called tripper that will ask the user the number of miles he has driven and the amount of gas he used. In the tripper script, write a subroutine called mileage that will calculate and return the user's mileage (miles per gallon). The number of miles driven and the amount of gas used will be passed as arguments. All variables should be my variables. Print the results. Prototype tripper.
2.Hotels are often rated by using stars to represent their score. A five-star hotel may have a king-size bed, a kitchen, and two TVs; a one-star hotel may have cockroaches and a leaky roof. Write a subroutine called printstar that will produce a histogram to show the star rating for hotels shown in the following hash. The printstar function will be given two parameters: the name of the hotel and the number of its star rating. (Hint: sort the hash keys into an array. Use a loop to iterate through the keys, calling the printstar function for each iteration.)
 %hotels=("Pillowmint Lodge" => "5",
          "Buxton Suites"    => "5",
          "The Middletonian" => "3",
          "Notchbelow"       => "4",
          "Rancho El Cheapo" => "1",
          "Pile Inn"         => "2",
         );

(OUTPUT)
Hotel                   Category
------------------------------------------
Notchbelow          |****         |
The Middletonian    |***          |
Pillowmint Lodge    |*****        |
Pile Inn            |**           |
Rancho El Cheapo    |*            |
Buxton Suites       |*****        |
------------------------------------------

Sort the hotels by stars, five stars first, one star last.

3.Write the grades program to take the course number and the name of a student as command-line arguments. The course numbers are CS101, CS202, and CS303. The program will include three subroutines:
  1. Subroutine ave to calculate the overall average for a set of grades.

  2. Subroutine highest to get the highest grade in the set.

  3. Subroutine lowest to get the lowest grade in the set.

Print the average, the highest score, and the lowest score. If there were any failures (average below 60), print the name, course number, and a warning to STDERR such as: Be advised: Joe Blow failed CS202. Send the name of the failing student and the course number to a file called failures. Sort the file by course number.

Use the AUTOLOAD function to test that each of the subroutines has been defined.


 

Previous Page Next Page