Could it be that it's too large for perl? or it could be a non-numeric variable in the data file, so how would I test for that?
You can get a result of "NaN" by doing addition *only* if one (or more) of the values being summed is a NaN.
I would therefore check the terms being added to see if any of them match the regex /^nan/i .