Comparing lists with List::Compare

Had to come up with a list of users on a medium-sized OID (Oracle Internet) directory who were not in any of a particular set of LDAP groups. Check out my solution using List::Compare for perl after the jump.

Going in I knew this would require generating a couple of lists: (a) all user entries on the directory; and (b) all uniquemembers in the subject groups. The latter would get sorted unique (using UNIX sort -u). Both lists would need to be trimmed of dn elements so only the RDN values remained (dn: or uniquemember: cn=exampleuser,cn=users,dc=example,dc=com to exampleuser, using perl as a text editor) and transformed to lower case (because OID makes all uniquemember dns lower case by default).

The last thing I’d need to do is actually compare the two lists. I had a vague notion about using UNIX diff for this, or maybe Kartik Subbarao’s ldifdiff.pl, but wasn’t really sure about the mechanics. Then I happened on this article over on Perl Monks, describing the use of Jim Keenan’s List::Compare module for perl.

Here’s the code I came up with do do the job, using List::Compare’s get_unique method:

#!/usr/bin/perl 
# cmplists.pl Read in two files and compare
# Created 9/28/10 by P Lembo
#
use strict;
use List::Compare;
	
my $HOME = $ENV{'HOME'};
	
my $infile1 = "$HOME/prod_users.txt";
my $infile2 = "$HOME/prod_grpmems.txt";
my $outfile = "$HOME/prod_not_grpmems.txt";
	
open FH1, "<$infile1" or die $!;
open FH2, "<$infile2" or die $!;
open FH3, ">$outfile" or die $!;
	
my @Llist = <FH1>;
my @Rlist = <FH2>;
	
my $lc = List::Compare->new(\@Llist, \@Rlist);
	
my @Lonly = $lc->get_unique;
	
foreach my $item(@Lonly) {
    print $item;
    print FH3 $item;
    
}
	
close FH3;
close FH2;
close FH1;
	
__END__;