My Code From Lab 4
#!/usr/bin/perl -w
use DBI;
use Getopt::Std;
use strict;
my (%opts,$db);

die "Bad option or missing option argument!\n" if !getopts("u:i:g:a:", \%opts);
die "Usage: $0 [-u username -i uid -g gid -a filename]!\n" if @ARGV != 0;
die "Must use exactly ONE option!\n" if keys(%opts) != 1;
die "Bad uid!\n" if exists($opts{i}) && $opts{i} =~ /\D/;
die "Bad gid!\n" if exists($opts{g}) && $opts{g} =~ /\D/;

$db = DBI->connect("DBI:CSV:f_dir=$ENV{HOME}/advperl") ||
   die "Can't connect to database!\n";

GetRecsByUsername($db, $opts{u})        if exists $opts{u};
GetRecsByUID($db, $opts{i})             if exists $opts{i};
GetRecsByGID($db, $opts{g})             if exists $opts{g};
MergeFile($db, $opts{a})                if exists $opts{a};


sub GetRecsByUsername
{
    my ($db, $username) = @_;
    my ($FindUser, $uid, $gid);

    $FindUser = $db->prepare("SELECT * FROM passwd
                              WHERE username = '$username'");
    $FindUser->execute() || die "Cannot execute username find!\n";

    die "Cannot find: $username!\n" 
         unless ($username, $uid, $gid)  = $FindUser->fetchrow_array;
    printf "%-30s %-4d %-4d\n", $username, $uid, $gid;
}


sub GetRecsByUID
{
    my ($db, $uid) = @_;
    my ($FindUid, $rows, $username, $gid);

    $FindUid = $db->prepare("SELECT * FROM passwd WHERE uid = $uid");
    $FindUid->execute() || die "Cannot execute user-id find!\n";
    die "Cannot find: $username!\n" 
         unless ($username, $uid, $gid)  = $FindUid->fetchrow_array;
    printf "%-30s %-4d %-4d\n", $username, $uid, $gid;
}


sub GetRecsByGID
{
    my ($db, $gid) = @_;
    my ($FindGid, $rowrefs, $rowref, $username, $uid);

    $FindGid = $db->prepare("SELECT * FROM passwd WHERE gid = $gid");
    $FindGid->execute() || die "Cannot execute group-id find!\n";
    $rowrefs = $FindGid->fetchall_arrayref();
    die "Cannot find group-id $gid!\n" if !@$rowrefs;

    foreach $rowref (@$rowrefs)
    {
       ($username, $uid, $gid) = @$rowref;
       printf "%-30s %-4d %-4d\n", $username, $uid, $gid;
    }
}

sub MergeFile
{
    my ($db, $file, $client) = @_;
    my ($ReadFile, $rows, $UserINlist, %UserDups, $username, $DupDetect,
        $duprows, $rowref, $uid, $gid, $RowInsert, $UidINlist, %UidDups);

    die "Database does not exist!\n" if !-e $file;
    $ReadFile = $db->prepare("SELECT username, uid, gid  FROM $file");
    $ReadFile->execute() || die "Cannot read merge file!\n";
    $rows = $ReadFile->fetchall_arrayref;

    $UserINlist = join(",", map {"'$_->[0]'"} @$rows);  ###   [0] is username!!
    $UidINlist  = join(",", map {$_->[1]} @$rows);      ###   [1] is uid!!
 
    $DupDetect = $db->prepare("SELECT username, uid FROM passwd
                               WHERE username IN ($UserINlist)
                                  OR gid IN ($UidINlist)");
    $DupDetect->execute() || die "Cannot look for duplicates!\n";

    while (($username, $gid) = $DupDetect->fetchrow_array)
    {
         $UserDups{$username} = 1;   ##  All username keys are duplicates!! 
         $UidDups{$gid} = 1;
    }

    foreach $rowref (@$rows)
    {
        ($username,$uid,$gid) = @$rowref;

        if (exists $UserDups{$username})
        {
            print "Duplicate username $username!\n";
            next;
        }

        if (exists $UidDups{$uid})
        {
            print "Duplicate uid $uid!\n"; 
            next;
        }

        $RowInsert = $db->prepare("INSERT INTO passwd (username,uid,gid)
                                   VALUES ('$username', $uid, $gid)");
        $RowInsert->execute() || die "Cannot insert row into database!\n";
    }
}