I am an experienced developer, but not in Perl. I usually learn Perl to crack a script, then I will forget it again until next time. Therefore, I seek advice from professionals.
This time I am building a series of data analysis scripts. In general, the simplified structure of the program is as follows:
01 my $config_var = 999; 03 my $result_var = 0; 05 foreach my $file (@files) { 06 open(my $fh, $file); 07 while (<$fh>) { 08 &analyzeLine($_); 09 } 10 } 12 print "$result_var\n"; 14 sub analyzeLine ($) { 15 my $line = shift(@_); 16 $result_var = $result_var + calculatedStuff; 17 }
In real life, there are about a dozen different config_var and result_var s.
These scripts differ mainly in the values โโassigned to config_var s. The main loop will be the same in each case, and analyzeLine() will be basically the same, but may have slight variations.
I can accomplish my task by making N copies of this code with slight modifications here and there; but this grossly violates all the rules of good design. Ideally, I would like to write a series of scripts containing only a set of config var initializations, followed by
do theCommonStuff;
Note that config_var (and its siblings) must be available for shared code, as well as result_var and its images, on which analyzeLine() performs some calculations.
Should I pack my "generic" code in a module? Create a class? Use global variables?
While not exactly golf for golf, I am looking for a simple, compact solution that will allow me to DRY and write code just for differences. I think that I would prefer not to output the code from a huge table containing all the configs, and, of course, not adapt it to use the database.
We look forward to your suggestions and thanks!
Update
Since people were asking, here is the real analyzeLine :
# Update stats with time and call data in one line. sub processLine ($) { my $line = shift(@_); return unless $line =~ m/$log_match/; # print "$1 $2\n"; my ($minute, $function) = ($1, $2); $startMinute = $minute if not $startMinute; $endMinute = $minute; if ($minute eq $currentMinute) { $minuteCount = $minuteCount + 1; } else { if ($minuteCount > $topMinuteCount) { $topMinute = $currentMinute; $topMinuteCount = $minuteCount; printf ("%40s %s : %d\n", '', $topMinute, $topMinuteCount); } $totalMinutes = $totalMinutes + 1; $totalCount = $totalCount + $minuteCount; $currentMinute = $minute; $minuteCount = 1; } }
Since these variables are largely interdependent, I think that a functional solution with separate calculations will not be practical. I apologize for the misleading people.