in reply to string truncation problem
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 } [download]