The full story is that I'm parsing some documents in Excel spreadsheets and I just wanted to simplify the expresions, so the list c('A1'),c('A'),c('B') just means that the value at the constant cell A1 is followed by the values of columns A and B at the "current row" in the details area, without having to include the iterator variable as in c('A',1),c('A',$i),c('B',$i) (which is also supported). The problem was that I always got the values at a fixed row based on the previous value of the iterator variable when it was not explicit as an argument, either as c('A',$i) or c("A$i").
Of course, there are also some helper functions to get formated values. For instance, d('A',1) internally calls c(@_) and returns a date properly formatted (and not an epoch number). As there could be many levels of nested functions, I decided to use a global variable and avoid the optional parameter.
Now I know that for over lists does a special treatment on the iterator variable, probably because of a perl 4 inheritance, and I should use a C-like for or a while construct when using a global variable as iterator from subroutines called from the loops, but for the moment, I'm using this construct:
my $row = 0;
for (2..10) {
$row = $_;
gen(d('A1'), c('A'), c('B'), t('C'), c('D')+c('E'));
}
I think it is much more clear and easy to maintain when the spreadsheet changes than:
my $row = 0;
for ($row = 2, $row <= 10; $row++) {
gen(d('A',1), c('A',$row), c('B',$row), t('C',$row), c('D',$row)+c('
+E',$row));
}
Now I see that if I had declared the global variable without an initial value of zero, an error should have been raised and saved many hours trying to identify why I got wrong values from the spreadsheet. >:-( |