Search code examples
linuxperlfileeol

Using Perl Win7 to write a file for Linux and having only Linux line endings


This Perl script is running on Win7, modifying a Clearcase config spec that will be read on a Linux machine. Clearcase is very fussy about its line endings, they must be precisely and only \n (0x0A) however try as I may I cannot get Perl to spit out only \n endings, they usually come out \r\n (0x0D 0x0A)

Here's the Perl snippet, running over an array of config spec elements and converting element /somevob/... bits into element /vobs/somevob/... and printing to a file handle.

        $fh = new FileHandle;
    foreach my $line (@cs_array)
        {
        $line =~ s/([element|load])(\s+\/)(.+)/$1$2vobs\/$3/g; 
        $line =~ s/[\r\n]/\n/g;  # tried many things here
        $fh->print($line);
        }
    $fh->close();

Sometimes the elements in the array are multi-line and separated by \n

element /vob1/path\nelement\n/vob2/path\nload /vob1/path\n element\n
/vob3/path 
load /vob3/path

When I look into the file written on Win7 in a binary viewer there is always a 0x0D 0x0A newline sequence which Clearcase on Linux complains about. This appears to come from the print.

Any suggestions? I thought this would be a 10 minute job...


Solution

  • Try

    $fh->binmode;
    

    Otherwise you're probably in text mode, and for Windows this means that \n is translated to \r\n.