PrimeLord has asked for the wisdom of the Perl Monks concerning the following question:
I didn't put the whole script because I figured that was a lot of information to read through as it is, but I think that should be enough to get the gist of what I am doing. Basically it reads all the files it finds into a today hash and all the files it found yesterday into a yesterday hash. And then compares the hashes and writes the differences into two different reports. Any suggestions on how I can optimize this?sub _todays_files { my ($host, $script_mode, $options) = @_; my %today; my $search_files = (split /:/, $options->{$script_mode})[0]; if ($host =~ /hosta/) { open IN, "find / $search_files -print |" or die "Error: Could not run find command:\n$! +"; while (<IN>) { chomp; $today{$_}++; } close IN or warn "Error: Could not close find command:\ +n$!"; } else { open IN, "find / -path '/usr/home' -prune -o $search_f +iles -print |" or die "Error: Could not run find command:\n$! +"; while (<IN>) { chomp; $today{$_}++; } close IN or warn "Error: Could not close find command:\ +n$!"; } return \%today; } sub _read_benchmark { my ($bench_dir, $host) = @_; my %yesterday; if (-e "$bench_dir/$host.benchmark") { open BENCH, "$bench_dir/$host.benchmark" or die "Error: Could not open $bench_dir/$host +.benchmark:\n$!"; while (<BENCH>) { chomp; $yesterday{$_}++; } close BENCH or warn "Error: Could not close $bench_dir/$ho +st.benchmark:\n$!"; } return \%yesterday; } sub _write_benchmark { my ($bench_dir, $host, $today, $user_uid, $user_gid) = @_; open BENCH, "> $bench_dir/$host.benchmark" or die "Error: Could not open $bench_dir/$host.benchma +rk for writing:\n$!"; for (sort keys %$today) { print BENCH "$_\n"; } close BENCH or warn "Error: Could not close $bench_dir/$host.bench +mark:\n$!"; chown $user_uid, $user_gid, "$bench_dir/$host.benchmark"; chmod 0640, "$bench_dir/$host.benchmark"; return; } sub _print_report { my ($dirs, $today, $yesterday, $options, $script_mode, $host) += @_; my $skip = join ('|', map { quotemeta } keys %$dirs); my $title = (split /:/, $options->{$script_mode})[2]; my $title_count = length($title); my $host_count = length($host); my $new_count = ($host_count + $title_count + 30); my $old_count = ($host_count + $title_count + 34); print NEW "########## New $title on $host ##########\n\n"; for (sort keys %$today) { print NEW "$_\n" unless (/^($skip)/) || exists $yester +day->{$_}; } print NEW "\n"; print NEW "#" x $new_count; print OLD "########## Removed $title on $host ##########\n\n"; for (sort keys %$yesterday) { print OLD "$_\n" unless (/^($skip)/) || exists $today- +>{$_}; } print OLD "\n"; print OLD "#" x $old_count; return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Memory Question
by dragonchild (Archbishop) on Feb 21, 2003 at 19:05 UTC | |
by l2kashe (Deacon) on Feb 21, 2003 at 20:03 UTC | |
by Limbic~Region (Chancellor) on Feb 21, 2003 at 22:40 UTC | |
|
Re: Memory Question
by derby (Abbot) on Feb 21, 2003 at 19:39 UTC | |
|
Re: Memory Question
by Thelonius (Priest) on Feb 21, 2003 at 20:57 UTC |