# TrafficLight.pm --- an abstraction of the traffic light

package TrafficLight;

# Properties:
# state: 0 for RED (stop); 1 for GREEN (go)

# Create a TrafficLight class instance (instantiation)
sub new {
	my $class = ref($_[0]) || $_[0];
	my $this = {};
	$this->{'state'} = 0;	# defaults to "stop"
	bless $this, $class;
	return $this;
}

# Get/set state: "stop" or "go"
sub state {
	my $this = shift;
	if (defined $_[0]) {
		if ($_[0] != 0 && $_[0] != 1) {
			die "TrafficLight::state(): Invalid argument.\n";
		}
		$this->{'state'} = $_[0];
		print (($this->{'state'})?"GREEN":"RED", " light is ON.\n");
	}
	return $this->{'state'};
}

1;
