in reply to Changing CRLF in Windows to LF for UNIX

The easiest way is to read the file line by line, printing each line after changing the newline. Using I/O layers makes the code even shorter:
#! /usr/bin/perl use warnings; use strict; open my $IN, '<:raw:crlf', shift or die $!; open my $OUT, '>:raw', shift or die $!; print {$OUT} $_ while <$IN>;

:raw turns off the CRLF automatic translation that is the default on MSWin (so the input doesn't really need the layers to be specified).

Call as win2nix input.txt output.txt.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Changing CRLF in Windows to LF for UNIX
by mabowden (Novice) on Oct 30, 2018 at 21:10 UTC
    I just found my problem, thanks in large part to your code. I had binmode attached to the initial (IN) file handle, but did not have it attached to the OUT handle. Adding "binmode OUT;" allows my original code to work. Thanks for the help!
Re^2: Changing CRLF in Windows to LF for UNIX
by mabowden (Novice) on Oct 30, 2018 at 21:01 UTC
    That worked, so thanks. But now I'm trying to figure out why my code didn't work earlier. I had binmode turned on, and I thought it defaulted to ":raw", and indeed that's what allowed me to get the Macintosh CR to show up solo at the end of each line. I did try to explicitly add ":raw" to the end, but that didn't help.
      Have you tried binmoding the OUT, too?

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        That was indeed my problem. There's nothing like fixing the problem, then unfixing it ...

      You should use 3 arg open FILEHANDLE,MODE,EXPR. I tried out the code and it does work for me.

      >> I'm coding in Windows (Windows 10) and trying to change a Windows created text file that ends in CRLF to just LF, so that I can upload the file and process it on a Unix-based site.

      If you are running the script in Windows, it will happen since when you open for write (without binmode or :raw or :unix), Perl will perform '\n' to (Windows) platform '\r\n'.