in reply to ^M chars in output file
You could set the input record separator (see $INPUT_RECORD_SEPARATOR or $/ in perlvar) to "\r\n", use chomp to remove the separator and then add the \n at the output stage. Alternatively, open the file in <:crlf mode which will remove the CR for you. In the code below I use split, ord and sprintf to show the individual characters as read and after chomping.
use strict; use warnings; open my $outFH, q{>}, \ my $dataFile or die qq{open: > scalar ref.: $!\n}; print $outFH qq{1,2,3\r\n}, qq{4,5,6\r\n}, qq{7,8,9\r\n}; close $outFH or die qq{close: > scalar ref.: $!\n}; { print qq{\nSetting input record separator to CRLF\n}; local $/ = qq{\r\n}; open my $inFH, q{<}, \ $dataFile or die qq{open: < scalar ref.: $!\n}; while( <$inFH> ) { print qq{Line $.\n}; print qq{ Original: }, qq{@{ [ map sprintf( q{%#.2x}, ord ), split m{} ] }\n}; chomp; print qq{ Chomped: }, qq{@{ [ map sprintf( q{%#.2x}, ord ), split m{} ] }\n}; } close $inFH or die qq{close: < scalar ref.: $!\n}; } print qq{\nOpening file in "<:crlf" mode\n}; open my $inFH, q{<:crlf}, \ $dataFile or die qq{open: < scalar ref.: $!\n}; while( <$inFH> ) { print qq{Line $.\n}; print qq{ Original: }, qq{@{ [ map sprintf( q{%#.2x}, ord ), split m{} ] }\n}; chomp; print qq{ Chomped: }, qq{@{ [ map sprintf( q{%#.2x}, ord ), split m{} ] }\n}; } close $inFH or die qq{close: < scalar ref.: $!\n};
The output.
Setting input record separator to CRLF Line 1 Original: 0x31 0x2c 0x32 0x2c 0x33 0x0d 0x0a Chomped: 0x31 0x2c 0x32 0x2c 0x33 Line 2 Original: 0x34 0x2c 0x35 0x2c 0x36 0x0d 0x0a Chomped: 0x34 0x2c 0x35 0x2c 0x36 Line 3 Original: 0x37 0x2c 0x38 0x2c 0x39 0x0d 0x0a Chomped: 0x37 0x2c 0x38 0x2c 0x39 Opening file in "<:crlf" mode Line 1 Original: 0x31 0x2c 0x32 0x2c 0x33 0x0a Chomped: 0x31 0x2c 0x32 0x2c 0x33 Line 2 Original: 0x34 0x2c 0x35 0x2c 0x36 0x0a Chomped: 0x34 0x2c 0x35 0x2c 0x36 Line 3 Original: 0x37 0x2c 0x38 0x2c 0x39 0x0a Chomped: 0x37 0x2c 0x38 0x2c 0x39
I hope this is helpful.
Cheers,
JohnGG
|
|---|