Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) 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:

given ($hash{'eventName'}) { when ('AudioLevel') {...} ... }
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:
my %keys = ( AudioLevel => [qw(StreamNumber StreamPosition AudioLevel)], Phoneme => [qw(StreamNumber StreamPosition Duration NextPhoneId F +eature CurrentPhoneId)], # ... ); if ($keys{$hash{'eventName'}}) { @hash{@{$keys{$hash{'eventName'}}}} = @_[2..$#_]; } else { die "unknown event $hash{'eventName'}"; }
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.
my %keys = ( ... Viseme => [ qw(StreamNumber StreamPosition Duration), { NextVisemeId => sub { DecodeSpeechVisemeType($_[5]) }, Feature => sub { DecodeSpeechVisemeFeature($_[6]) }, CurrentVisemeId => sub { DecodeSpeechVisemeType($_[7]) } ], ... ); if ($keys{$hash{'eventName'}}) { my @keys = @{$keys{$hash{'eventName'}}}; my $dynamic_keys = ref $keys[-1] ? pop @keys : {}; @hash{@keys} = @_[2..$#keys+2]; for my $dyn (keys %$dynamic_keys) { $hash{$dyn} = $dyanmic_keys->{$dyn}->(@_); } } else { die "unknown event $hash{'eventName'}"; }
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[0] =..., 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. :-)

In reply to Re: SAPI and Win32::OLE's broken WithEvents by Tanktalus
in thread SAPI and Win32::OLE's broken WithEvents by bulk88

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (14)
    As of 2018-03-20 14:59 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (254 votes). Check out past polls.