hi all
i am a java devoloper need to understand a perl scrip based on a project requirement. as i dont know any thing about the Perl i am posting my script here. some body please look in to it and help me out explaing what the script does
the script follows
#!/usr/bin/perl
#
# This program is called in an Analytics/Sector directory to create
# the Deal run time analytsics files within that sector.
# The script must be called with the proper directory, sector and product as arguments.
#
use warnings;
use strict;
use POSIX qw(strftime);
#
# Test to make sure the script is called with correct arguments.
#
($#ARGV == 0) || die "ERROR: Call with one argument: Input Directory \n";
#
# Define local variables.
#
my $InputDir = $ARGV[0];
my $FileNamePattern = $InputDir . "/*.txt"; # Pattern to match for input files.
my @FileNames; #
my $FileName; #
my $FileNameFlag; #
my $Count; #
my $DistribYearMonth; #
my $Header; # Header line of the input file.
my $Line; # Non-header line from the input file.
my @LineArray; # Non-header line from the input file.
my $OutLine; # Output line.
my $DealName; # Deal name, used to create the output directory.
my @DealNameDirs; # List of deal name directories.
my $OutFileName; # Output file name.
my $OutHeader; #
my $HeaderValue;
my @HeaderArray;
my %HeaderHash=();
my $now = strftime("%Y-%m-%d %R", localtime(time));
#
# Set up the output header.
#
$OutHeader= "LoanID" . "\t" .
"FICO" . "\t" .
"Occupancy" . "\t" .
"Purpose" . "\t" .
"Documentation" . "\t" .
"SFUnit" . "\t" .
"OrigRate" . "\t" .
"CurrRate" . "\t" .
"Margin" . "\t" .
"OrigLTV" . "\t" .
"CombLTV" . "\t" .
"OrigBalance" . "\t" .
"CurrBalance" . "\t" .
"FirstReset" . "\t" .
"IOTerm" . "\t" .
"PPTerm" . "\t" .
"DTI" . "\t" .
"LoanAge" . "\t" .
"Lien" . "\t" .
"PMILevel" . "\t" .
"ZipCode" . "\t" .
"StateBucket" . "\t" .
"PayStatus" . "\t" .
"PayHistory" . "\t" .
"Term" . "\t" .
"AmTerm" . "\t" .
"LoanNo" . "\n";
#
# Get a list of current deal name directories.
#
@DealNameDirs = glob "*";
foreach $DealName ( @DealNameDirs ){
chdir($DealName);
@FileNames = glob("*.txt");
foreach $FileName ( @FileNames ){
$FileNameFlag = $FileName . ".flag";
open(FlagHANDLE,">$FileNameFlag");
close(FlagHANDLE);
}
@FileNames = glob("*.old");
foreach $FileName ( @FileNames ){
unlink($FileName);
}
chdir("../.");
}
#
# For each .txt file in the New data directory, process the file.
#
@FileNames = glob $FileNamePattern;
foreach $FileName ( sort alphabetically @FileNames ){
print("$FileName \n");
open(InputHANDLE,$FileName) || die "Could not open $FileName";
# Get and process the header information
$Header=<InputHANDLE>;
chop($Header);
@HeaderArray=split(/\t/,$Header);
$Count=0;
foreach $HeaderValue (@HeaderArray){
$HeaderHash{$HeaderValue}=$Count;
$Count++;
}
# For each non-header line, process the data.
while ($Line = <InputHANDLE>) {
# Grab the deal name and distrib year month fields for this line.
chop($Line);
@LineArray=split(/\t/,$Line);
$DealName=$LineArray[$HeaderHash{'Deal Name'}];
$DistribYearMonth=$LineArray[$HeaderHash{'Distrib Year Month'}];
# If the deal name directory does not exist, create it.
(-d $DealName) || mkdir($DealName,0755);
# Create the output file name.
$OutFileName=$DealName . "/" . "DataRT" . $DistribYearMonth . ".txt";
# If the file name flag is set, move the old run-time file and delete the flag file.
$FileNameFlag = $OutFileName . ".flag";
if(-e $FileNameFlag){
unlink($FileNameFlag);
rename($OutFileName,$OutFileName . ".old");
}
# If the run-time file does not exist, create it and write header line.
if(!(-e $OutFileName)){
open(OutputHANDLE,">$OutFileName");
print OutputHANDLE "<HEAD>\n";
print OutputHANDLE "Version\t1.2\n";
print OutputHANDLE "Deal\t" . $DealName . "\n";
print OutputHANDLE "As Of\t" . $DistribYearMonth . "\n";
print OutputHANDLE "<LOG>\n";
print OutputHANDLE "Created\tCreator\n";
print OutputHANDLE $now . "\tPaul Check\n";
print OutputHANDLE "</LOG>\n";
print OutputHANDLE $OutHeader;
print OutputHANDLE "</HEAD>\n";
close(OutputHANDLE);
}
$OutLine= $LineArray[$HeaderHash{'Numeric Loan ID'}] . "\t" .
$LineArray[$HeaderHash{'FICO'}] . "\t" .
$LineArray[$HeaderHash{'Occupancy'}] . "\t" .
$LineArray[$HeaderHash{'Purpose'}] . "\t" .
$LineArray[$HeaderHash{'Document'}] . "\t" .
$LineArray[$HeaderHash{'SFUnit'}] . "\t" .
$LineArray[$HeaderHash{'Init Coupon'}] . "\t" .
$LineArray[$HeaderHash{'Curr Coupon'}] . "\t" .
$LineArray[$HeaderHash{'Margin'}] . "\t" .
$LineArray[$HeaderHash{'LTV'}] . "\t" .
$LineArray[$HeaderHash{'Comb LTV'}] . "\t" .
$LineArray[$HeaderHash{'Orig Amt'}] . "\t" .
$LineArray[$HeaderHash{'Balance'}] . "\t" .
$LineArray[$HeaderHash{'Reset Month'}] . "\t" .
$LineArray[$HeaderHash{'IO Term'}] . "\t" .
$LineArray[$HeaderHash{'PP Term'}] . "\t" .
$LineArray[$HeaderHash{'DTI'}] . "\t" .
$LineArray[$HeaderHash{'Loan Age'}] . "\t" .
$LineArray[$HeaderHash{'Lien'}] . "\t" .
$LineArray[$HeaderHash{'PMI Level'}] . "\t" .
$LineArray[$HeaderHash{'ZipCode'}] . "\t" .
$LineArray[$HeaderHash{'State Bucket'}] . "\t" .
$LineArray[$HeaderHash{'Delinq Stat'}] . "\t" .
$LineArray[$HeaderHash{'Pay History'}] . "\t" .
$LineArray[$HeaderHash{'Term'}] . "\t" .
$LineArray[$HeaderHash{'Amort Term'}] . "\t" .
$LineArray[$HeaderHash{'Loan No'}] . "\n";
open(OutputHANDLE,">>$OutFileName");
print OutputHANDLE $OutLine;
close(OutputHANDLE);
}
close(InputHANDLE);
}
#
# Delete the flag files.
#
foreach $DealName ( @DealNameDirs ){
chdir($DealName);
@FileNames = glob("*.txt.flag");
foreach $FileName ( @FileNames ){
unlink($FileName);
}
chdir("../.");
}
#
# Subroutine to compare alphbetically, used in sorting.
#
sub alphabetically {
my ($aa, $bb) = ($a, $b);
$aa =~ tr/A-Z/a-z/;
$bb =~ tr/A-Z/a-z/;
return $aa cmp $bb;
}
thanks and regards
R