Uri Guttman wrote: >>>>>> "SB" == Steve Bertrand <st...@ibctech.ca> writes: > > SB> # STRINGS > SB> case /^(comment|plan_type|description|desc|started| > SB> billing_address1|billing_address2|shipping_address1| > SB> shipping_address2|billing_company_name|shipping_company_name| > SB> shipping_town|billing_town)$/x > > SB> This is becoming extremely boring and tedious, as I have eight or nine > SB> similar but different case statements. I haven't been able to figure out > SB> a way to do this that will save me any time or work. Any suggestions on > SB> how I can classify the keys in a better (ie less boring and eye popping) > SB> way? > > you are going to hate me for this. the tool you just learned, dispatch > tables are perfect for this. you are matching full tokens so no regex is > needed. you have multiple sets of keys and you can point them to the > same code ref (you can use an anon sub as they are short chunks of > code). > > i told you dispatch tables are very useful! > > i leave how to code it as an exercise for you.
I knew that dispatch tables were the way to go, so I've been trying to figure out the best way to implement this with what I've learnt recently. After thinking for a while, here's what I've done: - extract all of the keys from all known data types - compiled a 'dispatch table' containing all keys, eliminating duplicates - wrote a small sub named after each type classification that performs the sanity check - manually inserted the appropriate coderef as a value to each known key in the dispatch table - no API changes were necessary. I simply replaced the switch logic with a table lookup and all is well Cheers! Steve ps. and no, I don't hate you for this. I welcome the feedback
smime.p7s
Description: S/MIME Cryptographic Signature