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

When addressing portability issues, it is usually an idea to check the value of $^O, which translates to $OSNAME if you use English. You can also get the terminal width using Term::Readkey and call the method GetTerminalSize. Note also that windows counts the characters and wraps irrespective of whether the characters are even visible.

use Term::ReadKey; use English qw( -no_match_vars ); sub Print { if ( $OSNAME =~ /^MSWin/) my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize()); # nb in this case, in above only need the first var # but keep the brackets for my $chunk (@_) { chomp $chunk; for my $line (split /\n/, $chunk) { print $line . (length($line) == $wchar ? '' : "\n"); } } } else { print @_; } }

One world, one people

Replies are listed 'Best First'.
Re^2: [OT] Why does newline in Windows print as having width?
by Anonymous Monk on Aug 20, 2018 at 10:14 UTC

    use English qw( -no_match_vars );

    See Devel::AssertOS Devel::CheckOS

    use Devel::CheckOS qw/ os_is /; use if os_is(qw/ Win32 /), 'MyFormatter::Win32', 'Print'; use if !!os_is(qw/ Win32 /), 'MyFormatter', 'Print'; ... Print(...);
      I studied these sources and they seem to be a wrapper for $^O. The docs for these modules justify their existence in two ways:-

      - that $^O is an ugly three characters.

      - that families like 'linux, bsd, unix,' are grouped into one.

      But the first justification would be better moved to the "use English" documentation, the pragma you seem to think is wrong and the second does not apply to the situation where the OP only wants to catch Windows. All that Devel::CheckOS does in this case is convert mswin32 to Win32.

      In addition I don't like some of the magic in these modules, requiring at one point a "no strict refs" for no very good reason.

      IMO, If you want to optmise $OSNAME, it would be better still to 'use English' and use 'something new' that adds a new variable like $OSFAMILY (*) to the set rather than interpreting $^O in a heavily over-engineered way (32k for the .tar.gz)

      (* update: and populated only once for the Perl process)

      One world, one people