I do not understand how the line consisting of dashes enter the if in which i (should only) have two numeric values.
The problem is that the regular expression in:
if($line2=~ m/(\d*)\s*([A-Z]*)\s*(\d*)-(\d*)/)
accepts anything that has at least one '-' in it. Remember that
\d* accepts
zero or more digits, and
\s* accepts
zero or more white-space characters, etc. So this regex will match zero or more digits, followed by zero or more white-space characters, followed by zero or more
[A-Z] characters, etc. The only thing that has to be present is a '-' to get a match.
I suspect that you would be perfectly happy with:
if($line2=~ m/(\d+)\s*([A-Z]+)\s*(\d+)-(\d+)/)
where you are using the regex to do two things at once: (a) recognise the lines that contain the information you wish to process further, and (b) parse those lines to extract that information.
If you are supremely confident that (a) the file you are processing always contains correctly formed lines, and (b) that your code recognises those correctly formed lines, then all will be well. In general I think it is wise to check the lines that are being rejected by the regex and warn about any whose format is not recognised. It's extra work to start with, but can save your bacon if some huge file at some future date contains broken data or stuff in a form you haven't catered for.