in reply to Format statement and 'if' loop

== is for numeric comparisons; you mean eq. Go read perlop.

Additionally: It doesn't really make any sense to close your output handle and then go on to process more rows. You probably want to use last to exit the loop and then close and print your footer outside.

The cake is a lie.
The cake is a lie.
The cake is a lie.