Justin_BSI has asked for the wisdom of the Perl Monks concerning the following question:
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!#### 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; }
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Implementing State pattern
by rhesa (Vicar) on May 02, 2006 at 23:57 UTC | |
|
Re: Implementing State pattern
by diotalevi (Canon) on May 02, 2006 at 23:25 UTC | |
by Justin_BSI (Acolyte) on May 02, 2006 at 23:39 UTC |