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:
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 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); }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |