|
|
| Perl: the Markov chain saw | |
| PerlMonks |
Re: Building an anonymous subroutineby stephen (Priest) |
| on Aug 12, 2002 at 23:22 UTC ( [id://189698]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
Sometimes a closure is an object waiting to happen. (And sometimes the reverse.) Consider: That defines your base class, where all the common code can live. Then, for each kind of encoder, you can define a subclass. For many subclasses, they'll be doing the same overall thing with variations, and you can just redefine the 'other_stuff' method. Otherwise, you can redefine 'encode' itself... but you'll still have 'common_stuff' to call if you want it.
Then, finally, you only need a factory subroutine to create the right type of encoder for each user preference:
And, finally, to get your new encoder and use it: There will be a speed hit, but it'll be faster than doing constant per-word checks, and easier to keep organized than closures-upon-closures-upon-closures. Note: Code is untested and I've been drinking Cabernet. stephen
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||