< Day Day Up > |
Listing 18.1 shows how a simple car could be represented in Perl. This listing should be saved as Car.pm . To create a class in Perl, a constructor must bless a reference into the class. What this means is a reference (it doesn't matter what type, most people use hash references) is going to be created within the new subroutine. The bless function will take that reference and associate it with the class as an instance. The new subroutine should then return that reference. Listing 18.1. First Part of the Car Class1: #!/usr/bin/perl -w 2: 3: use strict; 4: package Car; 5: 6: my $model_name = "RoadPerl"; 7: my $vin = 1; 8: 9: sub new { 10: my $car = { 11: vin => $vin++, 12: rpm => 0, 13: }; 14: bless $car; 15: }
There's no rule that says that the constructor for Car has to be called new; it's just convention. In fact, your class can have as many constructors as it wants, or none at all. But it's a little pointless without a constructor. Listing 18.2 continues our Car Class by providing methods to get the speed of the car and the VIN. Methods that are simply returning properties of a class are called accessor methods . Listing 18.2. Property Methods in the Car Class16: sub vin { 17: my($self)=@_; 18: return $self->{vin}; 19: } 20: sub speed { 21: my($self)=@_; 22: return $self->{rpm}/100; 23: }
The Car Class's next two methods, shown in Listing 18.3, control the car's internals and provide the Car's actions. Listing 18.3. Methods in the Car Class24: sub brake { 25: my($self)=@_; 26: $self->{rpm}=0; 27: print "Skreetch!\n"; 28: } 29: sub accelerate { 30: my($self,$value)=@_; 31: $self->{rpm}=$self->{rpm} + $value; 32: if ($self->{rpm} > 10) { 33: die "Engine blew up in " . $self->{vin}; 34: } 35: print "Vroom!\n"; 36: }
Finally, Listing 18.4 provides methods to get information about the entire Car Class. Because an object instance isn't required to use these, they're called class methods . Listing 18.4. Remainder of the Car Class37: sub model_name { 38: return $model_name; 39: } 40: sub models_sold { 41: return $vin-1; 42: }
You can call class methods in any of the following ways: Car->model_name(); # Probably the preferred style Car::model_name(); # Using the package name directly, see Hour 17 $car->model_name(); # Using an instance of Car works too. Using the Car ClassNow you have a car class. How do you use it? Using the class, it would look like this: my $newcar = new Car; So now $newcar contains a blessed reference into the car class. What does this mean? If you use that reference with the method operator -> , functions within that class will be called, like so: print $newcar->vin(); When Perl sees this it will call the vin subroutine inside of the Car class. As an added bonus it will pass a copy of that original reference as the first argument to the subroutine vin . If you want your car to accelerate, you'll need to pass an additional value indicating how much: print $newcar->accelerate(2); Again, Perl finds the class that $newcar has been blessed into, finds the accelerate subroutine, and calls it. It will be called with two arguments: the reference (as all methods are) and the value 2 . You know that internally, it will be used to increase the car's RPM. But from our perspective, it will just print Vroom! . Listing 18.5 is a small program to create a sort -of race. Several cars will be created, each one will accelerate
|