Perl Module Basics
The STORE() method is almost as simple:
# method called when scalar is written to
sub STORE {
my ($self, $val) = @_;
# return if the vent is already in the requested state
return $val if $val == $self >{vent_state};
# open or close vent
if ($val) {
open_vent($self >{compartment});
} else {
close_vent($self >{compartment});
}
# store and return current vent state
return $self >{vent_state} = $val;
}
STORE() receives two arguments, the object and the new value. The code checks to
see if it can return right away if the vent is already in the requested position. It
then calls the class methods close_vent() or open_vent() as necessary. Finally, it
returns the set value. STORE() methods should return their new value so that chained
assignment works as expected:
$foo = $thermo = 1; # $foo == 1
It's possible to call object methods using a tied variable. There are two ways to
get access to the underlying object. First, it's returned by tie(). Second, it can be
retrieved from a tied variable using the tied() routine. For example, say you added
a method to the BOA::Thermometer class called fan() to turn on and off a fan
inside the vent. Client code could call this method as follows:
$thermo_obj = tie $thermo, BOA::Thermometer , compartment => 10;
$thermo_obj >fan(1); # turn on the fan
This will also work:
tie $thermo, BOA::Thermometer , compartment => 10;
tied($thermo) >fan(1); # turn on the fan
Using additional object methods, tied modules can provide enhanced functionality
without giving up the simplicity of a tied variable interface.
59
59
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