Depending on platform, the \n sequence is converted by perl to:

Unix: octal \012 hex 0xA dec 10 LF may be \n Dos: octal \015\012 hex 0xD0xA dec 13 10 CRLF may be \r\n Max: octal \015 hex 0xD dec 13 CR may be \r

Although perl works for you trying to allow you to just use \n as your newline delimiter and let it sort the platform dependent details, many common *internet protocols* specify the \015\012 sequence and unfortunately the values of Perl's \n and \r are not reliable since they can and do vary across platforms.

I suspect that $textfield is named from its HTML source so you will probably want to use a truly portable solution like this:

$textfield =~ s/\015\012|\015|\012//g; If you prefer hex to octal :-) $textfield =~ s/\xD\xA|\xD|\xA//g;

If you are confused by the \012 or \xA notation all this is saying to perl is what I want you to match is the ASCII char decimal 10 == octal 12 == hex A == binary 1010

In expanded commented /x form: $textfield =~ s/ # substitute \015\012 # a CRLF sequence (DOS, MIME...) | # or \015 # a lone LF (mac) | # or \012 # a lone LF (unix) / # with literal '' /xg; # /x allow comments, /g do globally
tachyon

In reply to Re: How do I remove carriage returns from scalars? by tachyon
in thread How do I remove carriage returns from scalars? by bazfaz

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.