Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newlines on Windows

Reply
Thread Tools

Newlines on Windows

 
 
void.no.spam.com@gmail.com
Guest
Posts: n/a
 
      09-27-2008
I thought that on Windows, if you want to output a carriage return,
you have to use \r\n instead of just \n. So in my perl script that
generates a file, I put a \r\n after every line, but when I open the
file in an editor, I see a ^M after each line. This is with
ActivePerl 5.8.8.
 
Reply With Quote
 
 
 
 
Chris Haffenstedt
Guest
Posts: n/a
 
      09-27-2008
schrieb:
> I thought that on Windows, if you want to output a carriage return,
> you have to use \r\n instead of just \n.


So it's like in PHP. In Perl you haven't to do so. Just use \n and Perl
takes care for the rest, on Windows, Linux, Macintosh and all the rest.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-27-2008
"" <> wrote:
>I thought that on Windows, if you want to output a carriage return,
>you have to use \r\n instead of just \n.


Neither nor. You are confusing two different layers.

To output a carriage return you have to print the ASCII controll
character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
the CR character for ASCII which in turn is a subset of Windows-1252,
ISO-Latin-1, and many other encodings.

"\n" on the other hand in Perl is a logical line end, which will be
translated automatically into the correct line end sequence for the
current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
for Mac.

So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
Probably not what you want.

> So in my perl script that
>generates a file, I put a \r\n after every line, but when I open the
>file in an editor, I see a ^M after each line. This is with


Well, you got what you added.

jue
 
Reply With Quote
 
void.no.spam.com@gmail.com
Guest
Posts: n/a
 
      09-27-2008
On Sep 27, 3:34*pm, Jürgen Exner <jurge...@hotmail.com> wrote:
> "void.no.spam....@gmail.com" <void.no.spam....@gmail.com> wrote:
> >I thought that on Windows, if you want to output a carriage return,
> >you have to use \r\n instead of just \n.

>
> Neither nor. You are confusing two different layers.
>
> To output a carriage return you have to print the ASCII controll
> character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
> the CR character for ASCII which in turn is a subset of Windows-1252,
> ISO-Latin-1, and many other encodings.
>
> "\n" on the other hand in Perl is a logical line end, which will be
> translated automatically into the correct line end sequence for the
> current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
> for Mac.
>
> So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
> Probably not what you want.
>
> > So in my perl script that
> >generates a file, I put a \r\n after every line, but when I open the
> >file in an editor, I see a ^M after each line. *This is with

>
> Well, you got what you added.
>
> jue


Thanks for the explanation.
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      09-27-2008
wrote:

> I thought that on Windows, if you want to output a carriage return,
> you have to use \r\n instead of just \n. So in my perl script that
> generates a file, I put a \r\n after every line, but when I open the
> file in an editor, I see a ^M after each line. This is with
> ActivePerl 5.8.8.


This is an issue with what you use to edit the file. It's inserting
hidden line feed characters (junk) in the script.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      09-27-2008
* wrote in comp.lang.perl.misc:
>I thought that on Windows, if you want to output a carriage return,
>you have to use \r\n instead of just \n. So in my perl script that
>generates a file, I put a \r\n after every line, but when I open the
>file in an editor, I see a ^M after each line. This is with
>ActivePerl 5.8.8.


See `perldoc perlport` for an in-depth discussion of line endings.
--
Björn Höhrmann · private.php?do=newpm&u= · http://bjoern.hoehrmann.de
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      09-28-2008
On 2008-09-27 19:47, <> wrote:
> On Sep 27, 3:34*pm, Jürgen Exner <jurge...@hotmail.com> wrote:
>> "void.no.spam....@gmail.com" <void.no.spam....@gmail.com> wrote:
>> >I thought that on Windows, if you want to output a carriage return,
>> >you have to use \r\n instead of just \n.

>>
>> Neither nor. You are confusing two different layers.
>>
>> To output a carriage return you have to print the ASCII controll
>> character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
>> the CR character for ASCII which in turn is a subset of Windows-1252,
>> ISO-Latin-1, and many other encodings.
>>
>> "\n" on the other hand in Perl is a logical line end, which will be
>> translated automatically into the correct line end sequence for the
>> current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
>> for Mac.
>>
>> So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
>> Probably not what you want.

[...]
>
> Thanks for the explanation.


It is worth noting that the translation Jürgen talks about happens only
during file I/O. Inside of a perl script, "\n" is always a single
character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
also on MacOS X). When writing to a file with the :crlf I/O layer enabled
(which is the default on Windows, but you can turn it off with binmode
or an argument to open), the single "\n" is converted to "\015\012". The
reverse conversion is done when reading a file with the :crlf layer.

(Perl can do other conversions on I/O, too, e.g., character set
conversions, compression, etc.)

hp
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-28-2008
"Peter J. Holzer" <hjp-> wrote:
>It is worth noting that the translation Jürgen talks about happens only
>during file I/O. Inside of a perl script, "\n" is always a single
>character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
>also on MacOS X). When writing to a file with the :crlf I/O layer enabled
>(which is the default on Windows, but you can turn it off with binmode
>or an argument to open), the single "\n" is converted to "\015\012". The
>reverse conversion is done when reading a file with the :crlf layer.


To be crystal clear on that: did you mean "writing to a file" or
"writing to a file handle"?
Like in writing to STDOUT, but having the shell redirect into a file?

jue
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      09-28-2008
Jürgen Exner schreef:

> your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.


On Windows, \n is CR-LF, not LF-CR.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      09-28-2008
On 2008-09-28 09:38, Jürgen Exner <> wrote:
> "Peter J. Holzer" <hjp-> wrote:
>>It is worth noting that the translation Jürgen talks about happens only
>>during file I/O. Inside of a perl script, "\n" is always a single
>>character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
>>also on MacOS X). When writing to a file with the :crlf I/O layer enabled
>>(which is the default on Windows, but you can turn it off with binmode
>>or an argument to open), the single "\n" is converted to "\015\012". The
>>reverse conversion is done when reading a file with the :crlf layer.

>
> To be crystal clear on that: did you mean "writing to a file" or
> "writing to a file handle"?


File handle.

hp
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing newlines from string on windows (without replacing) Tom Python 5 07-26-2009 08:57 PM
Newlines in Datagrid chrismichaelgardner@hotmail.com ASP .Net 2 05-04-2009 03:06 PM
How to always write Windows style newlines to a file? Wes Gamble Ruby 7 12-14-2006 04:13 PM
newlines in textboxes headware ASP .Net 1 09-22-2004 08:10 PM
YAML and Windows newlines Jamis Buck Ruby 2 06-18-2004 03:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57