Hello Monks. I am hoping some of you can take a look at a program recently wrote and maybe make some suggestions on how it can be improved. The function of the program is to transfer copies of several reports from several sites each day. The program is run every 15 minutes out of a cron job between midnight and eight am.

Essentially it looks to see if the reports are completed at each site and if they are copies them over. I am using ssh commands to do the listings and copies. One of my concerns is that I am generating a ton of SSH connections and I would like to find a good way to cut that down. My Perl skills are improving, but are still a long way from being very good. So please be gentle. Any suggestions you might have would be greatly appreciated. Thank you. Below is a copy of the code.

use strict; use Time::Local; sub _init_values { my ($seconds, $minutes, $hours, $day, $month, $year) = localtim +e; my $date = sprintf("%04d%02d%02d", $year + 1900, $month + 1, $d +ay); my $report_dir = "/reports/directory"; my $config_dir = "/config/directory"; return $date, $report_dir, $config_dir, $hours; } sub _get_list { my ($date, $report_dir, $config_dir, $hours) = @_; my %reports; if ($hours == 00) { open IN, "$config_dir/report_master_list" or die "$!"; while (<IN>) { chomp; $reports{$_}++; } close IN or warn "$!"; _get_reports($date, $report_dir, $config_dir, \%reports) +; } elsif ($hours == 08) { open IN, "$config_dir/report_temp_list" or die "$!"; while (<IN>) { chomp; $reports{$_}++; } close IN or warn "$!"; open OUT, ">> /disk2/daily_report" or die "$!"; print OUT "\n\nDaily Reports That Were Unable To Be Tran +sferred\n"; print OUT "--------------------------------------------- +---\n\n"; for (sort keys %reports) { print OUT "$_\n"; } print OUT "\n\n"; unlink "$config_dir/report_temp_list" or warn "$!"; } else { open IN, "$config_dir/report_temp_list" or die "$!"; while (<IN>) { chomp; $reports{$_}++; } close IN or warn "$!"; _get_reports($date, $report_dir, $config_dir, \%reports) +; } } sub _get_reports { my ($date, $report_dir, $config_dir, $reports) = @_; my %site_map = qw( site1 fullsite1 site2 fullsite2 site3 fullsite3 site4 fullsite4 site5 fullsite5 site6 fullsite6 ); for (sort keys %$reports) { my ($report_name, $site) = split /\./; my @files = `ssh host.$site ls $report_dir`; for my $file (@files) { chomp $file; if ($file =~ /$report_name/ && $file =~ /$date/) +{ my $status = `ssh host.$site cat $report_d +ir/$file`; if ($status =~ /End of Report/) { my $transfer = `scp host.$site:$rep +ort_dir/$file /disk2/$site_map{$site}/Reports`; delete $reports->{$_}; } } } } open OUT, "> $config_dir/report_temp_list" or die "$!"; for (sort keys %$reports) { print OUT "$_\n"; } } my ($date, $report_dir, $config_dir, $hours) = _init_values; _get_list($date, $report_dir, $config_dir, $hours);


Thanks again for any help you can offer. Sorry if some of the long lines make the script kind of hard to read.

-Prime

In reply to Code Review by PrimeLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.