in reply to Code Review

This is a quick refactoring of your _get_list function. It's not guaranteed to work (it's untested) and it could use more refactoring, but it gives you the idea of what's going on. What I've done is move duplicate code into its own subroutine and taken code unrelated to the actual function and moved it into a subroutine that is (as far as I can tell) descriptively named. This makes the logic of the _get_list function much cleaner and easier to read.

Again, this is only a rough start, but it gives you an idea of where to go from here. Note that the actual logic of _get_list has been altered slightly so as to avoid a bit of duplication.

sub _get_list { my ($date, $report_dir, $config_dir, $hours) = @_; my %reports; if ($hours == 0) { %reports = _sum_report_entries($config_dir, 'report_master_list'); } else { %reports = _sum_report_entries($config_dir, 'report_temp_list'); if ($hours == 8) { _write_non_transferred_reports( %reports ); unlink "$config_dir/report_temp_list" or warn "$!"; return; # this *seems* to duplicate the logic! } _get_reports($date, $report_dir, $config_dir, \%reports); } sub _write_non_transferred_reports { my %reports = @_; open OUT, ">> /disk2/daily_report" or die "$!"; print OUT "\n\nDaily Reports That Were Unable To Be Transferred\n"; print OUT "------------------------------------------------\n\n"; for my $report (sort keys %reports) { print OUT "$report\n"; } print OUT "\n\n"; close OUT; } sub _sum_report_entries { my ($config_dir,$list_file) = @_ my $report = "$config_dir/$list_file"; my %sums; open REPORT, "<", $report or die "Cannot open ($report) for reading: + $!"; while ( chomp (my $line = <REPORT>) ) { $sums{$line}++; } close REPORT or warn "Cannot close ($report): $!"; return %sums; }

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: Code Review
by PrimeLord (Pilgrim) on Dec 13, 2002 at 15:10 UTC
    Thanks for the reply! The changes you have suggested do make the code a lot more readable. Plus it has given me an idea on some similar changes in another script I am working on. Thanks again!

    -Prime