Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Repeating Code - there has GOT to be a better way!

by cleverett (Friar)
on Mar 31, 2010 at 17:43 UTC ( #832102=note: print w/ replies, xml ) Need Help??


in reply to Repeating Code - there has GOT to be a better way!

You need to make it so you can run the same code for every one of the files. The thing that changes every time is %<name>Cmd.

One question to ask is: must all the %<name>Cmd hashes in the .pm files have different names?

If yes, you need alias %<name>Cmd onto %Cmd or something like that. This is kind of cool, but not recommended to do routinely.

If no, then just rename the %<name>Cmd hashes in each of the .pm files to %Cmd.

Either way, you can process all the files in a loop or a subroutine (or both) with code that looks something like this:

our %Cmd; do "db/$name" . 'Cmd.pm'; $ApidOffset = $Application{$name}{AppApidOffset}; foreach $cmd (keys %Cmd) { if (int($fssbCmd{$cmd}{fixed_pattern}) <= 15) { $opcode = sprintf("0%x",$Cmd{$cmd}{fixed_pattern}); } else { $opcode = sprintf("%x",$Cmd{$cmd}{fixed_pattern}); } $apid = sprintf("%x",$ApidBase+$ApidOffset); $cmdData{$cmd} = {opcode => "0x$opcode", apid => "0x$apid"}; };
Hope this helps.


Comment on Re: Repeating Code - there has GOT to be a better way!
Download Code
Replies are listed 'Best First'.
Re^2: Repeating Code - there has GOT to be a better way!
by jedikaiti (Friar) on Mar 31, 2010 at 17:49 UTC

    I have no control over what is in the .pm files, so I can't make them all the same name.

    Thanks!

    Kaiti
    Swiss Army Nerd

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://832102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2015-07-30 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls