|Think about Loose Coupling|
Repeating Code - there has GOT to be a better way!by jedikaiti (Hermit)
|on Mar 31, 2010 at 16:58 UTC||Need Help??|
jedikaiti has asked for the
wisdom of the Perl Monks concerning the following question:
Today's mission, if you choose to accept it, is to help me deal with some very repetitive code. I have a script that works beautifully, but has a problem. It repeats this code about 11 times:
Wherever you see <name>, that indicates where the code differs each time. I have a bunch of files with the naming convention <name>Cmd.pm, and each file contains a hash called <name>Cmd. In each case, I need to get at the array in the file and do the exact same thing to it.
So I have this code repeating right now, once for each file. I know there has to be a better way, but all I'm coming up with so far is naming a variable with a variable, which seems generally a bad idea, and I'm not too sure it would work anyway.
So here I am, dear Monks - hoping someone knows a better way. A way that will work without repeating the same code 11 times. A way that won't induce heavy drinking and swearing in anyone working with this code after me.
Also, here's an example of the type of data that is being sucked in from the <name>Cmd.pm files:
As always, THANK YOU!
Swiss Army Nerd