in reply to How do I remove carriage returns from scalars?

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

Replies are listed 'Best First'.
(tye)Re: How do I remove carriage returns from scalars?
by tye (Sage) on May 31, 2001 at 20:05 UTC

    No, in DOS, "\n" is "\012". It only becomes "\015\012" when written to a file (and then only if that file handle is not in binmode -- and this is done by the C RTL, not by Perl). Even under Unix, "\n" is turned into "\015\012" when written to many devices (just not when written to files).

            - tye (but my friends call me "Tye")