Chapter 3
Now to set values in the hash, a two argument form of the accessor is used:
# heading at (10,20,100) at 50 kph
$probe >heading(x => 10);
$probe >heading(y => 20);
$probe >heading(z => 100);
$probe >heading(speed => 50);
There are some limitations to using Class::Struct. First, it can't be used to
create child classes. Class::Struct goes to some truly amazing lengths to prevent
this.
10
Second, if you need to do anything interesting when the constructor gets
called, you'll need to write your own new().
Using Class::MethodMaker
When Class::Struct isn't enough, Class::MethodMaker
11
comes to the rescue.
Class::MethodMaker supports all the same functionality as Class::Struct and a
whole lot more. Class::MethodMaker can generate a wide variety of accessors that
cover not only object methods, but also useful class methods. I'll cover the basic
functionality, but you'll need to read the module documentation to discover the
more esoteric bits. There's a lot in there!
Class::MethodMaker differs from Class::Struct in that you don't just tell it what
kind of data you'll be storing in your attributes; you also get to pick what kind of
accessor to provide. This also applies to the constructor, which Class::Struct doesn't
even let you name! Here's an example of using Class::MethodMaker to generate
some simple scalar accessor mutators and a constructor called new():
package BOA::Probe;
use Class::MethodMaker
new => new ,
get_set => [ qw(id model contents heading status) ];
Class::MethodMaker works by taking arguments to the use statement in key value
pairs. The key is a method in the Class::MethodMaker module, and the value is
either a single string or a reference to an array of strings. These strings are the
names of the methods to be created. After the preceding call, the eleven methods
are created two for each of the five attributes given for the get_set key and new().
The two methods created for each get_set attribute are a normal accessor
mutator for the attribute and a method to clear the attribute called clear_id(),
10. Class::Struct ties your @ISA to a private class that croaks on STORE()! How rude.
11. Available on CPAN. Class::MethodMaker was written by Peter Seibel and is being refurbished
and maintained by Martyn J. Pierce.
90
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved