<*.txt>
does not read a line from a file, even if you say so in a comment. It runs
glob '*.txt'
i.e. the while loop iterates over the file names, not over their contents. Use empty <>
to iterate over all the files.
BTW, instead of $header = undef
, you can use undef $header
.