Exercises for Chapter 4

 4.1
Write a Perl script whose input consists of a set of URLs, one per line, where the first is a complete URL and the remainder are partial, and whose output consists of the full URLs obtained by resolving the partial ones relative to the first. (See The HTML Sourcebook if you are unsure about partial URLs and how they are resolved.)
[Test data for 4.1]

§4.2
Consider the fashionable subject of JavaBeans. (It doesn't actually matter if you have never heard of JavaBeans.) Their only feature of interest for the present exercise is that a rigid convention is used to classify the names that programmers define when they construct a JavaBean. If PropName is what they call a property name, then there will be some methods associated with it. In particular, there will be a method whose name is of the form getPropName, which is called a get method for PropName. Similarly, setPropName is a set method for PropName. If PropName is the name of a Boolean property, though, its get method is called isPropName, and is a test method. If EvName is something called an event name, then addEvNameListener is a listener adding method, while removeEvNameListener is a listener removing method. Property and event names always begin with an upper-case letter. Write a Perl script that takes a file of method names and classifies them, identifying the property and event names. Your script should produce output like the following:
addTimerListener :  listener adder for Timer event
setFileName :  set method for FileName property
isFileOpen : test method for Boolean property FileOpen


[Hints/Solution | Test data for 4.2]

 4.3
Re-do the previous exercise by writing individual subroutines for each category of method name, which return true of false depending on whether $ARG belongs to the corresponding category.
 4.4
The syntax diagram compiler in this chapter just gives up and dies when it finds an error in its input. Real compilers usually try to recover from errors and parse the remaining input so that they find as many errors as possible on a single run. Modify the compiler so that it attempts some error recovery. (One way to do this is by using eval and exceptions, but it isn't the only way.)
 4.5
Perl does not have a switch or case statement, unlike most contemporary languages. Investigate different ways of simulating such a statement using the various control flow structures introduced in this chapter.
[Hints/Solution for 4.5]


Exercises for previous chapter | Exercises for next chapter | Perl: The Programmer's Companion