Are you reading a file that was created with windows? Perl on windows translates the crlf line separators to "\n" (Removes the ^M) as it reads the file. You can do the same thing on other systems by specifying the crlf "layer" on your open statement or with binmode. The following code demonstrates that each line is read with the newline, but without the carriage return.
use strict;
use warnings;
use Test::More tests=>4;
my $windows_file =
"abc". "\r\n"
."def". "\r\n"
."ghi". "\r\n"
;
my $in_file = \$windows_file;
my $string;
open my $fh, "<:crlf", $in_file;
$string = <$fh>;
ok( $string eq "abc\n", "line 1" );
$string = <$fh>;
ok( $string eq "def\n", "line 2" );
$string = <$fh>;
ok( $string eq "ghi\n", "line 3" );
ok( eof($fh), "end-of-file");
|