Google


ADBRITE ads links
You are here: CodeIdol.com > Perl > Intermediate Perl > Objects With Data

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

Intermediate Perl



12.1. A Horse Is a Horse, of Course of Courseor Is It? Let's look at the code used in Chapter 11 for the Animal classes and Horse classes: { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "...



12.2. Invoking an Instance Method The method arrow can be used on instances, as well as names of packages (classes). Let's get the sound that $tv_horse makes: my $noise = $tv_horse->sound; To invoke sound, Perl first note...



12.3. Accessing the Instance Data Because we get the instance as the first parameter, we can now access the instance-specific data. In this case, let's add a way to get at the name: { package Horse; @ISA = qw(Animal); sub soun...



12.4. How to Build a Horse If we constructed all our horses by hand, we'd most likely make mistakes from time to time. Making the "inside guts" of a Horse visible also violates one of the principles of OOP. That's good if we're a veterinarian but ...



12.5. Inheriting the Constructor Was there anything specific to Horse in that method? No. Therefore, it's also the same recipe for building anything else inherited from Animal, so let's put it there: { package Animal; sub speak {...



12.6. Making a Method Work with Either Classes or Instances All we need to fix this is a way to detect whether the method is called on a class or an instance. The most straightforward way to find out is with the ref operator. This o...



12.7. Adding Parameters to a Method Let's train our animals to eat: { package Animal; sub named { my $class = shift; my $name = shift; bless \$name, $class; } sub name { my $either = shift; ref $either ...



12.8. More Interesting Instances What if an instance needs more data? Most interesting instances are made of many items, each of which can, in turn, be a reference or another object. The easiest way to store these items is often in a hash...



12.9. A Horse of a Different Color Having all horses be brown would be boring. Let's add a method or two to get and set the color: ## in Animal sub color { my $self = shift; $self->{Color}; } sub set_color { my $self = shif...



12.10. Getting Our Deposit Back Because of the way the code is written, the setter also returns the updated value. Think about this (and document it) when we write a setter. What does the setter return? Here are some common variations: The update...



12.11. Don't Look Inside the Box We might have obtained or set the color outside the class simply by following the hash reference: $tv_horse->{Color}. However, this violates the encapsulation of the object by exposing its i...



12.12. Faster Getters and Setters Because we're going to play nice and always call the getters and setters instead of reaching into the data structure, getters and setters are called frequently. To save a teeny-tiny bit of time, we mig...



12.13. Getters That Double as Setters Another alternative to the pattern of creating two different methods for getting and setting a parameter is to create one method that notes whether or not it gets any additional arguments. If the arguments are...



12.14. Restricting a Method to Class-Only or Instance-Only Setting the name of an unnameable generic Horse is probably not a good idea; neither is calling named on an instance. Nothing in the Perl method definition says "this is a class meth...



12.15. Exercise You can find the answer to this exercise in "Answer for Chapter 12" in the Appendix. 12.15.1. Exercise [45 min] Give the Animal class the ability to get and set the name and color. Be sure that your result works under use stric...

read more: Exercise

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > Perl > Intermediate Perl > Objects With Data
   
Related tags







Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......






© CodeIdol Labs, 2007