in reply to [OT] Why does newline in Windows print as having width?

Hello Lotus1,

I assume that the windows for both your Linux and your Windows command prompts are exactly 80 characters wide? If so, the following one-liner:

perl -we 'print "-" x 80; <STDIN>;'

on Linux (actually Cygwin, in my case) will show the cursor at the end of the line; but the equivalent script on Windows:

perl -we "print '-' x 80; <STDIN>;'

shows the cursor on the following line. This means that terminating the line with a carriage return \r will take the cursor back to the beginning of the line of hyphens on Linux, but on Windows it cannot do so because the cursor is already on a new line befor the carriage return is printed. Likewise for Perl’s \n character: printing a newline at the beginning of a line results in the extra blank line you are seeing.

If you know that your command window will always be exactly 80 characters wide, you can get the output you want on Windows by simply omitting the newline character altogether:

print "-" x 80; print "2\n";

— but, of course, this is not portable either across platforms or across Windows command prompts of different widths.

You may be able to achieve what you want using the Win32::Console module (but so far I haven’t figured out how to use it :-( Why is the Synopsis missing from its documentation??).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: [OT] Why does newline in Windows print as having width?
by Lotus1 (Vicar) on Aug 18, 2018 at 05:09 UTC

    Hi Athanasius,

    You wrote:

    If you know that your command window will always be exactly 80 characters wide, you can get the output you want on Windows by simply omitting the newline character altogether:

    The third section of the test program I posted does exactly that. Often I print things to both the console and a log file so it's annoying that they don't look the same if I try to print a partition across the whole line. Limiting my output to one less than the line width takes care of it but I was curious if other monks noticed or cared. I realize it's a very minor issue.

    Update: I tried your one-liners with <STDIN>. That's a good demonstration of the issue. If someone wanted to print a full line of characters and then use \r or \b to overwrite characters they could in Linux but not in Windows. If I modify your one-liner for Windows like the following then I can overwrite characters but as soon as the last column is written to then the line advances.

    perl -we "print q(-)x79,qq(\b\b);<STDIN>;"

    Your demonstration shows the issue is that cmd.exe will advance as soon as the last column is printed to whereas Linux doesn't advance until one character past the last column. This shows it has nothing to do with one or two characters for '\n'.