When dealing with a problem like this, you know that you want to write “maintainable” code that will Do The Right Thing™ without being flummoxed by (inevitable...) slight differences (glitches) in the input data. So, the general structure of the program might be:
open_files();
write_table_header();
while (my $row = read_next_record()) {
output_table_row($row);
}
write_table_footer();
close_files();
The next “interesting” part is to consider the various forms that the input lines may take. Such as:
- End of file has been reached.
- Blank line.
- tag: value
- None of the above (garbage line)
The read_next_record() routine treats the input as consisting of a stream of one of these four line-types, and it must work correctly in all cases including empty-file.