Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Searching for duplication in legacy code (updated)

by haukex (Archbishop)
on Nov 23, 2016 at 11:26 UTC ( #1176409=note: print w/replies, xml ) Need Help??

in reply to Searching for duplication in legacy code

Hi yulivee07,

I've done a bit of work with PPI and there is a chance it could be useful to you. This was an interesting question to me so I went off and whipped something up (Update: that means please consider this a beta) that finds identical subs, perhaps it's useful to you. PPI could also be used for more powerful identification of duplicated code.

Update: I split the two example files out of the script and turned it into a usable standalone version. Apparently PPI isn't keeping track of the filenames, so instead of figuring out exactly why that is I just added the filename tracking myself; otherwise the code is basically the same as the previously posted version.


#!/usr/bin/env perl use warnings; use strict; sub add { # I am a comment my $x = shift; my $y = shift; my $sum = $x + $y; return $sum; } sub subtr { return shift - shift; } sub foo { return 'foo'.shift }


#!/usr/bin/perl use warnings; use strict; sub add { my $x = shift; my $y = shift; my $sum = $x + $y; return $sum; } sub subtract { return shift-shift; # Hello, World } sub foo { my $x = shift; return "foo$x"; }

And the script:

#!/usr/bin/env perl use warnings; use strict; use PPI; use Digest::SHA qw/sha1_hex/; die "Usage: $0 FILENAMES\n" unless @ARGV; my %subs; for my $filename (@ARGV) { my $doc = PPI::Document->new($filename); $doc->index_locations; for my $sub (get_subs($doc)) { my $key = sha1_hex(delete $sub->{code}); $sub->{file} = $filename; push @{ $subs{$key} }, $sub; } } #use Data::Dump 'pp'; pp \%subs; # Debug for my $val (values %subs) { next unless @$val>1; print "Potential Duplicates:\n"; for my $sub (@$val) { printf "\tsub %s in file %s, line %d, col %d\n", $sub->{name}, $sub->{file}//"unknown", $sub->{loc}[0], $sub->{loc}[1]; } } sub get_subs { my ($ppi_node) = @_; my @subs; for my $sub (@{ $ppi_node->find('Statement::Sub') }) { next if $sub->forward; my $str = $sub->block->content; my $doc = PPI::Document->new(\$str); # remove "insignificant" elements, note this isn't perfect, # e.g. some significant whitespace may be removed $doc->prune( sub { ! $_[1]->significant } ); push @subs, {name=>$sub->name, loc=>$sub->location, code=>$doc->serialize}; } return @subs; }


$ ./ Potential Duplicates: sub add in file, line 5, col 1 sub add in file, line 5, col 1 Potential Duplicates: sub subtr in file, line 11, col 1 sub subtract in file, line 11, col 1

Hope this helps,
-- Hauke D

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176409]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2023-03-20 11:49 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.