I have perl scripts I make for different tasks and I slowly add to them as needed based on additional requirements. Being a novice at perl scripting I know many of them are not as efficient as they probably could be.

I've linked one below, everything works, no errors or warnings but it's messy and it feels like I repeated a couple tasks for lack of a better idea of how to combine them.

Pasted below, any and all helpful suggestions are appreciated. I commented most of the stuff to sorta explain the point but feel free to say so if something is unclear.

use DirHandle (); use File::Spec (); use strict; use warnings; # Sets basic globals my $now = localtime; my $dir = "C:/Data"; my $dirL = "C:/DataL"; my $fCount=0; my $fCountL=0; # Sorts files in $dir and sets $latest->{file} with newest file. my $latest = (sort {$b->{mtime} <=> $a->{mtime}} map {{mtime => -M $_, file => $_}} <$dir/*>)[-1]; my $newM = (stat $latest->{file})[9]; # Sorts files in $dir and sets $oldest->{file} with oldest file. my $oldest = (sort {$a->{mtime} <=> $b->{mtime}} map {{mtime => -M $_, file => $_}} <$dir/*>)[-1]; my $oldM = (stat $oldest->{file})[9]; # Opens $dir and counts number of files opendir(DIR, $dir); LINE: while(my $FILE = readdir(DIR)) { next LINE if($FILE =~ /^\.\.?/); $fCount++; } closedir(DIR); # Opens $dirL and counts number of files opendir(DIRL, $dirL); LINE: while(my $FILE2 = readdir(DIRL)) { next LINE if($FILE2 =~ /^\.\.?/); $fCountL++; } closedir(DIRL); # Changes Newest/Oldest file display in email based on if any files ar +e found. my $latestF =''; my $oldestF =''; my $noFiles = 'Data: Empty directory, no files found that need to be p +rocessed.'; my $noFilesL = 'DataL: Empty directory, no files found that need to be + processed.'; if (($latest->{file}) || ($oldest->{file})){ $latestF = 'Newest File: '. $latest->{file}. ' with a timestamp of + '. scalar localtime $newM; $oldestF = 'Oldest File: '. $oldest->{file}. ' with a timestamp of + '. scalar localtime $oldM; $noFiles = ''; } exit;

In reply to Looking for some assistance in cleaning up a perl script by shadowfox

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.