I wrote a perl program to read in a directory containing SQL, and update column names,
based on a file read in containing the conversion mappings. I am a little confused
about what's going on in the loop where I read the convertpairs array. The splitting
of $arrayelement based on '=' seems to be working, and the array prints out ok. But
the line
print "$pair[0] <-> $pair[1]\n";
isn't working, hence the line
$filecontents =~ s/$pair[0]/$pair[1]/g;
isn't working. Can someone spot my logic error?
#!/usr/bin/perl -w
%readsuffixes = ("sql","yes");
$convertfile = 'c:\uf\work\convert.inf';
$sourcedirname = 'c:\uf\work\sqrin';
$outdirname = 'c:\uf\work\sqrout';
#--------------------------------------------------
#
#Load array with conversion pairs
#from conversion file. Load upper,lower, and proper
#case versions
#
#File format: EMPL_RCD#=EMPL_RCD
#
#--------------------------------------------------
$/='\n';
open(CONVERT, $convertfile) or die "Can't find $convertfile : $! \n";
while( $line = <CONVERT> ) {
$pairs = $line;
chomp($pairs);
push @convertpairs, uc($pairs);
push @convertpairs, lc($pairs);
$pairs = lc($pairs);
$pairs =~ s/([a-z]+)/\u$1/g;
push @convertpairs, $pairs;
}
#--------------------------------------------------
#
#Read input directory, apply all regexes in array
#to file. File is "slurped" in one gulp.
#
#--------------------------------------------------
undef $/;
opendir(DIR,$sourcedirname) or die "Can't open $sourcedirname: $! \n";
@files = readdir(DIR);
foreach $file (@files) {
if((-e "$sourcedirname$file") || (-e "$sourcedirname\\$file")) {
$file =~ /([^.]*)\.(\w*)/;
$suffix = lc($2);
if(exists($readsuffixes{$suffix})) {
print "$sourcedirname\\$file ";
$filename = "$sourcedirname\\$file";
open(INFILE,$filename) or die "Can't open $filename\n";;
$outfilename = ">$outdirname\\$file";
open(OUTFILE,$outfilename) or die "Can't open $filename\n";;
print "$outfilename\n";
$fileswritten += 1;
$filecontents = <INFILE>;
foreach $arrayelement(@convertpairs) { #
@pair = split /=/, $arrayelement; #
print "@pair\n"; # prints array ok
print "$pair[0] <-> $pair[1]\n"; # only prints
$filecontents =~ s/$pair[0]/$pair[1]/g; #
}
print OUTFILE $filecontents;
}
close(INFILE);
close(OUTFILE);
}
}
closedir(DIR);
print "\n Files written: $fileswritten\n";