I would imagine that your line-based approach will - once you exclude the standard-for-your-coding-style lines above - show you most of your copy-and-paste.

I'd actually start from the other end, looking for infrequently-occuring-but-nonunique lines. A line which is used in exactly 2 or 3 places is more likely to be part of a block of cut and paste than one used in 100.

When you are refactoring, a nice option you have in perl is to make use of first class functions/closures/anonymous subs.

This allows you to have more "fuzzy matching" between blocks of similar-but-not-identical code. You can pull out the shared boilerplate as a new method/sub and pass into it a closure which does the 'bit which is different for each occurence'.

It's a lightweight alternative to putting together an inheritance hierarchy to share code, which you may see in design patterns etc. For example:

sub print_tree { my $tree = shift; print_tree($tree->left); print $tree->value; print_tree($tree->right); } sub sum_tree { my $tree = shift; return sum_tree($tree->left) + $tree->node + sum_tree($tree->right); }
could become (sorry, this is untested - also, this exact example might be better as a Tree class, which probably already exists in CPAN, etc etc., but I hope the point survives):
sub walk_tree { my $tree = shift; my $per_node = shift; walk_tree($tree->left); $per_node->($tree->node); walk_tree($tree->right); } sub print_tree { my $tree = shift; walk_tree($tree, sub { print $_[0]; }); } sub sum_tree { my $tree = shift; my $total = 0; walk_tree($tree, sub { $total += $_[0]; }); return $total; }

In reply to Re: Refactoring tools for copy/paste jobs by jbert
in thread Refactoring tools for copy/paste jobs by rhesa

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.