ADBRITE ads links
You are here: CodeIdol.com > Perl > Intermediate Perl > Introduction To Objects
Intermediate Perl
| 11.1. If We Could Talk to the Animals
Obviously, the castaways can't survive on coconuts and pineapples alone. Luckily for them, a barge carrying random farm animals crashed on the island not long after they arrived, and the castaways began ...
|
|
| 11.2. Introducing the Method Invocation Arrow
A class is a group of things with similar behaviors and traits. For now, let's say that Class->method invokes subroutine method in package Class. A method is the object-oriented version ...
|
|
| 11.3. The Extra Parameter of Method Invocation
The invocation of:
Class->method(@args)
attempts to invoke the subroutine Class::method as:
Class::method('Class', @args);
(If it can't find the method, inheritance kicks in, ...
|
|
| 11.4. Calling a Second Method to Simplify Things
We can call out from speak to a helper method called sound. This method provides the constant text for the sound itself:
{ package Cow;
sub sound { 'moooo' }
sub speak {
my $class =...
|
|
| 11.5. A Few Notes About @ISA
This magical @ISA variable (pronounced "is a" not "ice-uh") declares that Cow "is a" Animal.[*] Note that it's an array, not a simple single value, because on rare occasions it makes sense to have more than one p...
|
|
| 11.6. Overriding the Methods
Let's add a mouse that can barely be heard:
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "!\n";
}
}
{ package Mouse;
@ISA = qw(Animal);
...
|
|
| 11.7. Starting the Search from a Different Place
A better solution is to tell Perl to search from a different place in the inheritance chain:
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound...
|
|
| 11.8. The SUPER Way of Doing Things
By changing the Animal class to the SUPER class in that invocation, we get a search of all our superclasses
(classes listed in @ISA) automatically:
{ package Animal;
sub speak {
my $class = shif...
|
|
| 11.9. What to Do with @_
In that last example, had there been any additional parameters to the speak method (like how many times, or in what pitch for singing, for example), the parameters would be ignored by the Mouse::speak method. If we w...
|
|
| 11.10. Where We Are So Far
So far, we've used the method arrow syntax:
Class->method(@args);
or the equivalent:
my $beast = 'Class';
$beast->method(@args);
which constructs an argument list of:
('Class', @args)
an...
|
|
| 11.11. Exercises
You can find the answers to these exercises in "Answers for Chapter 11" in the Appendix.
11.11.1. Exercise 1 [20 min]
Type in the Animal, Cow, Horse, Sheep, and Mouse class definitions. Make it work with use strict. U...
|
|
You are here: CodeIdol.com > Perl > Intermediate Perl > Introduction To Objects
|
|
Related tags
Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......
|
|