in reply to string truncation problem

In this case $txt results in a pretty big string with newlines. So I want to filter every line ...

If $txt is a big string with embedded newlines, then why are you using it to open a filehandle?

Wouldn't

foreach my $line ( split("\n", $txt) ) { # do something to filter $line }
do what you need?