Closing this question. Will drink red bull. Sleep. Code and come back with brand spanking new question with unit test cases.
UPDATE: The new file is here
Also the config file is here
I refactored the code again:
sub getColumns {
open my $input, '<', $ETLSplitter::configFile
or die "Error opening '$ETLSpliter::configFile': $!";
my $cols;
while( my $conline = <$input> ) {
chomp $conline;
my @values = split (/=>/, $conline);
if ($ETLSplitter::name =~ $values[0] ) {
$cols = $values[1];
last;
}
}
if($cols) {
@ETLSplitter::columns = split (':', $cols);
}
else {
die("$ETLSplitter::name is not specified in the config file");
}
}
This code always dies here die("$ETLSplitter::name is not specified in the config file");.
Another clue is that if I change split (':', $cols); to split (/:/, $cols); I get this error.
perl -wle "
use modules::ETLSplitter;
\$test = ETLSplitter->new('cpr_operator_metric_actual_d2', 'frame/');
\$test->prepareCSV();"
syntax error at modules/ETLSplitter.pm line 154, near "}continue"
Compilation failed in require at -e line 2.
BEGIN failed--compilation aborted at -e line 2.
FINAL POST FOR THIS QUESTION: Based on your latest updates, I believe the following code illustrates how there is no problem with using
/:/as the first argument tosplit. It also points out that it is easier to read code when one uses arguments to functions rather than relying on global variables:There is a lot of cruft in that code. Here is my interpretation of what you are trying to do:
UPDATE: Given your recent remark about regex special characters in patterns, if you are going to use them in the pattern to split, make sure to quote them. There is also a chance that
$ETLSpliter::namemight contain other special characters. I modified the code to deal with that possibility.ANOTHER UPDATE:
So, the pattern indeed is
/=>/based on your comment below. Then:No errors … No warnings Therefore, there is something else that is going on which you insist on not showing us.
Other Remarks:
Use lexical filehandles and let perl tell you what errors it may encounter rather than presuming.
Declare variables in the smallest applicable scope.
No need to assign
$_to$conlinein the body of the loop when you can do that in thewhilestatement.In the original code, you were not putting anything in
@columnsor doing anything useful with$colData.Tone down the rhetoric. Computers work on the principle of GIGO.
Looking at the code at the link you posted, it looks like you are not aware that you can do:
Further, it looks like you are using a package where hash would have done the job:
Finally, you do realize
Spliteris incorrect. ITYM:Splitter.