http://www.perlmonks.org?node_id=832102


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.

Replies are listed 'Best First'.
Re^2: Repeating Code - there has GOT to be a better way!
by jedikaiti (Hermit) 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