#### a driver for the car
{ package Driver;
sub new {
my $class = shift;
my $vars = {};
$vars->{ALERTNESS} = 0;
$vars->{STATE} = dstatenorm->new();
bless ($vars, $class);
return $vars; }
sub alertness {
my $vars = shift;
my $state = $vars->{STATE};
if(@_) { $vars->{ALERTNESS} = shift; }
return $state->alertness( $vars->{ALERTNESS} ); }
1; }
#### driver state normal
{ package dstatenorm;
sub new {
my $class = shift;
my $vars = {};
$vars->{"NAME"} = "Normal";
bless ($vars, $class);
return $vars; }
sub alertness ($) {
my $curAlertness = shift;
return $curAlertness; }
1; }
When I call the "alertness" routine on the driver, I get dstatenorm=HASH(0x1832734). It looks like I'm getting close to the return I want, but I'm not sure where or how or if I have to dereference to make this work.
Thanks in advance!
Update: Found it. I was shifting and the first value is the class itself. Shifting twice did the trick, so I guess I have to access @_ directly?
You didn't include an accessor in dstatenorm to fetch the NAME.
package dstatenorm;
...
sub name {
my $self = shift @_;
return $self->{NAME};
}Then in your code, request the name of the state.
print $driver->alertness->namePS: don't use prototypes especially when writing object oriented code.
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
That being said, thank you for reminding me about the accessor and I will quit prototyping in my OO code (I'm used to Java, so pardon me.)
I suspect you want something like this for the implementation of alertness:
sub alertness {
my $self = shift;
if( @_ ) {
$self->{STATE} = shift;
}
return $self->{STATE};
}
perlmonks.org content © perlmonks.org and diotalevi, Justin_BSI, rhesa
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03