|There's more than one way to do things|
Re: SAPI and Win32::OLE's broken WithEventsby Tanktalus (Canon)
|on May 07, 2012 at 18:13 UTC||Need Help??|
A couple of comments on the code.
First, in Events, you have a massive if/elsif/.../else. And every item is checking the same thing - the event name. This looks like a perfect use for given/when:
Except that you're doing the same thing in each clause (except for the default/unknown case). So a hash of arrays would likely work simpler:
I think that'll produce more concise and, more importantly, more readable code. Of course, it's missing important bits where you call other functions, and this is definitely solvable (especially with anonymous code refs in your %keys hash, assuming that the assignment code here looks for them and calls them). You may prefer given/when as it's less of a change from how you're currently thinking, but I generally prefer code refs.
I don't think that's too bad. A bit more difficult than if nothing was dynamic, but that's kind of the price you pay for that dynamicism.
Second is your @table for the DecodeSpeechVisemeType function. You may be better off putting the @table inside your function and calling it a state variable instead of a my. You can still initialise it once this way, but gets rid of some extra braces. But it also means that you need to learn to initialise arrays as part of their declaration. Instead of my @table; $table =..., say my @table = ( 'silence', 'ae ax ah', ... );. I'm actually surprised what you have is working, because my understanding is that your initialisation for @table wouldn't be called until after the Win32::OLE->MessageLoop() returns.
Finally, DecodeSpeechVisemeFeature - again with the if/elsif/else. Use a lookup hash. Or, in this case, a lookup array.
Warning: all code above untested. :-)