my $state = 128; # start state, binary: 1000 0000 my $lightmask = 16; # 0001 0000 my $bellmask = 8; # 0000 1000 sub togglelight { $state = $state ^ $lightmask; $port->write("lookclear(); } sub bellon { if (not ($state & $bellmask)) { $state = $state ^ $bellmask; $port->write("lookclear(); } } sub belloff { if ($state & $bellmask) { $state = $state ^ $bellmask; $port->write("lookclear(); } }