I've tucked this code away to add to Coolbar.pm when I next get back to it.
For both of your examples I was getting an error on exit - invalid methods on global destruction. Both were failing in onDeactivate event - to get round it I had to add the following:
return unless $_[0]; to the first line of the event handler. I'm on 5.6.1, latest code line. Cheers, jez.