I just got a new client that has a bunch of Perl scripts in three almost identical copies.

The story is that they used to have set of scripts dealing with one kind of input. When they started to receive a slightly different set of input - from another source - they copied the scripts and slightly modified some of them to cope with the new input. Then this happened again so now they have 3 sets of almost identical code.

Naturally no unit tests are around.

In addition some of the subroutines were also copied among the files of the original set so I have some duplication there too.

Luckily all of the use strict and warnings and almost no globals so the code is not bad at all.

What would be the best approach to deal with this?

I thought it would be nice to track down the identically named subroutines in the codebase and then recognize if the subroutines are the same or nearly the same, showing the diff between the subroutines.

For extra bonus it would be nice if I could easily unite two almost identical scripts into a module and a scrip. (and what if I have 3 copies...).

So this is a real issue I have now - or rather I had as I manually worked through it in the past 2 days but actually this could be a nice tool and a nice plugin to an IDE.

So let me also add my promotional link here to the Vertical Metre of Beer 3 - The Padre Plugin Contest! I'd be glad to see people starting to write a plugin that can help us handle such a situation.


In reply to Refactoring copy-pasted code by szabgab

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.