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


In reply to Re: ^M chars in output file by johngg
in thread ^M chars in output file by locust

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.