I was recently handed a project to take over some in-production code who's owner left the company, document it, fix any "bad stuff", and have my group start supporting it. The code is 6 Perl scripts and a shell script that runs them. I want rewrite this as one Perl script and simplify everything, and I'm running into problems untangling what filename is being used for what at what time. There are lots of STDOUT and STDERR redirects from the shell script. For example:

FILE_LIST=`ls *.wrk` for FILE in $FILE_LIST do logit "COPYING $FILE TO $TGT_DIR" cp $FILE $TGT_DIR logit "PROCESSING $FILE FOR GROUP1" perl /some/other/script.pl <$FILE >>TMPOUT 2>>err.rpt rm -f $FILE done

So something happens to the files in the CWD, the output is concatenated to TMPOUT, and later more processing is done on the files copied to $TGT_DIR, which are just the original .wrk files. Like so:

cd $TGT_DIR FILE_LIST=`perl /rename/by/id.pl $FILE_NAME` for FILE in $FILE_LIST do if perl /some/compliance/checks.pl $FILE 2>/dev/null >$FILE.ERR then cp -f $FILE archive cp -f $FILE out else logit "$FILE WAS MALFORMED:\n `cat $FILE.ERR`" mail -s "FILE $FILE INCOMPLETE, NOT SENT" $NOTIFY <<EOF FILE $FILE NOT SENT BECAUSE: `cat $FILE.ERR` EOF fi

I'm wondering if my time would be better spent creating a Perl script to replace the shell script, and import the Perl scripts as functions, or tracing everything that each script is doing, and build a new script that reduces everything to the essentials. For example, /some/other/script.pl and /some/compliance/checks.pl both do compliance checks for different groups in my company, and copy files if they pass to the appropriate directories. These could be combined into one script that compliance checks everything in one pass of the file, checks the IDs on the fly and does what /rename/by/id.pl would normally do.

I could spend a week or so trimming the 500 line monstrosity down to around 100 lines of just-the-essentials, or I could spend two days refactoring and documenting, and leave it looking like a giant, bloated mess. Each Perl script is also its own breed of hell, e.g.:

$FILENAME=shift; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks)= stat($FILENAME); ($fsec,$fmin,$fhour,$fmday,$fmon,$fyear,$fwday,$fyday,$fisdst) = local +time ($mtime); $fyear=$fyear+1900; $fmon++; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year=$year+1900; $mon++; open (INFILE,$FILENAME); while (<INFILE>) { chomp; if (m/(^[0-9]{2})/) { $RecordCount++; $STATS{"$1"}++; @Record=split(",",$_); if ($Record[0] eq '01') { $CREATE_DATE="20$Record[3]"; $DATESTRING=sprintf("%04d%02d%02d",$year,$mon,$mday); if (($DATESTRING-$CREATE_DATE)>2){ $WARNINGS{"01 Date looks old"}++; }; }; # etc.

Nice date delta function. Need I go on? I don't want code like that being my code and to be charged with supporting it. My question to the community is: What would you do? Have you ever been in a similar situation? How did it turn out?


In reply to Refactor and simplify by delirium

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.