class Cat
properties
$name
$color
/properties
method meow
print "meow\n"
/method
method printDetails
print "Cat.name\n"
print "Cat.color\n"
/method
/class
###################################
main
$cat = new Cat (name "Fred", color "white")
$cat.meow
$cat.printDetails
/main
Exercise
In analogy to the example above, create a class for another type of pet besides cat or dog (see below).
class Animal properties $name $color /properties method printDetails print "Animal.name\n" print "Animal.color\n" /method /class ################################### class Cat ISA Animal properties $name $color /properties method speak print "meow\n" /method /class ################################### class Dog ISA Animal properties $name $color /properties method speak print "woof\n" /method /class ################################### main $cat = new Cat(name "Fred", color "white") $dog = new Dog(name "Max", color "black") @pets = ($cat,$dog) foreach $pet (@pets) $pet.printDetails $pet.speak /foreach /main
Exercises
Add your pet from the previous exercise to the Animal class.
Let users input a type of animal (Cat, Dog, etc) and then print the sound they make. Hint: "ref $pet" returns the class to which $pet belongs ("ref" stands for "reference" or "refers to"). For example, if $pet is a cat then "ref $pet" equals Cat. But the user has to ask for "Cat" not "cat". Of course, you could use a regular expression with "ignore case" to compare the user input with ref $pet...
A module is a class (or "package" in Perl terminology) stored in a file by itself and named "class.pm". The last executed line in a module should be "return 1;". A class can be used in a different file by including "use class;" at the beginning of the file.
Exercise
Save Animal, Cat, Dog and the additional pet as modules by themselves. To use them you can either include "use" statements for all four of them in the main file or you use Cat, Dog and the other pet in the main file and Animal in Cat, Dog and the other pet. Try both possibilities.