sid_com
sid_com

Reputation: 25117

Win32 terminal: unexpected printing behavior

Where do the additional empty lines come from when printing this script in a windows terminal?

use strict;
use warnings;
use 5.10.0;
use Term::Size::Any qw(chars);

my $w = ( chars( \*STDOUT ) )[0];
my $string = "Y" x $w;

say $string;
say $string;
say $string;
say $w;

The output from the MSWindows console:

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

80

The output from the Linux console:

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
80

Upvotes: 2

Views: 373

Answers (3)

justintime
justintime

Reputation: 3631

Different terminals and terminal emulators/ consoles work differently if you write to the last column on the screen.

  • On windows, the cursor goes to the the 1st column of the next line, thus creating an illusion of an extra LF being printed.
  • On Linux the cursor effectively at an imaginary column to the the right of the screen. So when you send an LF it takes you to the next line.

This program sleeps between writing the long line and the LF

    use strict;
    use warnings;
    use 5.10.0;

    my $w = shift // 80 ;
    my $string = "Y" x $w;
    $|++;   # flush buffer on every write
    print $string ;
    print "\n" ;

    print $string ;
    sleep 2 ;
    print "\n" ;
    print "Finished\n" ;

On windows you see the following while sleeping.

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

[bottom of screen]

Whilst on Linux

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
[bottom of screen]

There is probably more to it than this, but hopefully this will satisfy your curiosity!

Upvotes: 2

ikegami
ikegami

Reputation: 386386

I am interested in why the script behaves differently on Windows.

Because the two terminals you are using are different programs.

Upvotes: 0

vips
vips

Reputation: 362

Instead of 'say' use 'print'. I think output will be same on both Linux and Windows.

Upvotes: 0

Related Questions