#>>if Record Type is vehicle---------------<<
if($filename =~ m/$infile3/){
#>>process vehicle records
my $vehicle_file = $infile3;
open VIINFILE, "<$vehicle_file" or die "Couldn't open file handle -- $vehicle_file for read. $!\n";
#logNorm("");
#>>remove file extention
$infile3 =~ s/\.dat//i;
open VIOUTFILE, ">$infile3.$expext" or die "Couldn't open file handle -- $infile3.$expext for writing : $!\n";
#logNorm("");
open VIOUTFILE2, ">$infile3.$outext" or die "Couldn't open file handle -- $infile3.$outext for writing : $!\n";
#logNorm("");
#>>loop through vehicle Records
while(<VIINFILE>){
my ($record_type, $vehicle_id, $incident_num, $lname, $fname,
$mname, $suffix, $dob, $age, $gender, $race,
$dl,$dlstate, $home_addr, $home_city, $state, $home_zip,
$home_phone, $empl_addr, $emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name
) = split /\t/, $_;
#>>search for the word removed in name fields
if(grep (/$pattern/i,$lname)){
#>>transform/replace the field
$lname = $newtext;
$fname = $newtext;
$mname = $newtext;
$suffix = $newtext;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname, $suffix,
$dob, $age, $gender, $race, $dl, $dlstate,
$home_addr, $home_city, $state, $home_zip,
$home_phone, $empl_addr, $emp_city, $emp_state,
$emp_zip, $emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record removed:\n";
print "$_";
#>>print to temp file
print VIOUTFILE "$_" or die "Error writing $infile3.$expext: $!\n";
}elsif(grep (/$pattern/i,$fname)){
$lname = $newtext;
$fname = $newtext;
$mname = $newtext;
$suffix = $newtext;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname, $suffix,
$dob, $age, $gender, $race, $dl, $dlstate,
$home_addr, $home_city, $state, $home_zip,
$home_phone, $empl_addr, $emp_city, $emp_state,
$emp_zip, $emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record removed:\n";
print "$_";
#>>print to temp file
print VIOUTFILE "$_" or die "Error writing $infile3.$expext: $!\n";
}elsif(grep (/$pattern/i, $mname)){
$lname = $newtext;
$fname = $newtext;
$mname = $newtext;
$suffix = $newtext;
#logNorm("");
#>>reconstruct the record in tab-delimited
#>> format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip, $emp_phone,
$org_name);
#>>print to screen STDOUT
print "vehicle Record removed:\n";
print "$_";
#>>print to temp file
print VIOUTFILE "$_" or die "Error writing $infile3.$expext: $!\n";
}elsif(grep (/$pattern/i, $suffix)){
$lname = $newtext;
$fname = $newtext;
$mname = $newtext;
$suffix = $newtext;
#logNorm("");
#>>reconstruct the record in tab-delimited
#>> format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record removed:\n";
print "$_";
#>>print to temp file
print VIOUTFILE "$_" or die "Error writing $infile3.$expext: $!\n";
}elsif($lname !=~ grep (/$pattern/i, $lname) ){ #>> Not removed
#>>transform/replace the field
$lname;
$fname;
$mname;
$suffix;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record:\n";
print "$_";
#>>print to temp file
print VIOUTFILE2 "$_" or die "Error writing $infile3.$outext: $!\n";
}elsif($fname !=~ grep (/$pattern/i, $lname) ){ #>> Not removed
#>>transform/replace the field
$lname;
$fname;
$mname;
$suffix;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record:\n";
print "$_";
#>>print to temp file
print VIOUTFILE2 "$_" or die "Error writing $infile3.$outext: $!\n";
}elsif($mname !=~ grep (/$pattern/i, $lname) ){ #>> Not removed
#>>transform/replace the field
$lname;
$fname;
$mname;
$suffix;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record:\n";
print "$_";
#>>print to temp file
print VIOUTFILE2 "$_" or die "Error writing $infile3.$outext: $!\n";
}elsif($suffix !=~ grep (/$pattern/i, $lname) ){ #>> Not removed
#>>transform/replace the field
$lname;
$fname;
$mname;
$suffix;
#logNorm("");
#>>reconstruct the record in tab-delimited format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip,
$emp_phone, $org_name);
#>>print to screen STDOUT
print "vehicle Record:\n";
print "$_";
#>>print to temp file
print VIOUTFILE2 "$_" or die "Error writing $infile3.$outext: $!\n";
}
}
close VIINFILE or die "Couldn't close file handle: $!\n";
#logNorm("");
close VIOUTFILE or die "Couldn't close file handle: $!\n";
#logNorm("");
close VIOUTFILE2 or die "Couldn't close file handle: $!\n";
#logNorm("");
}
Refactorings
No refactoring yet !
Alex Satrapa
May 30, 2011, May 30, 2011 20:21, permalink
Start with the obvious refactoring - extracting the copy-and-pasted "reconstruct the record in tab-delimited" line into a subroutine.
#>>reconstruct the record in tab-delimited
#>> format
$_ = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip, $emp_phone,
$org_name);
# Convert to a function:
sub construct_td_line {
my $td_line = join("\t", $record_type, $vehicle_id,
$incident_num, $lname, $fname, $mname,
$suffix, $dob, $age, $gender, $race, $dl,
$dlstate, $home_addr, $home_city, $state,
$home_zip, $home_phone, $empl_addr,
$emp_city, $emp_state, $emp_zip, $emp_phone,
$org_name);
return $td_line;
}
There's a lot of repletion in my code and the code blocks seem to be lengthy. Can someone look at the algorithm and see if it can be re-factored.