Question

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...

Was it helpful?

Solution

Try

$fh->binmode;

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

OTHER TIPS

You are running afoul of the :crlf IO Layer that is the default for Perl on Windows. You can use binmode after the fact to remove this layer, or you can open the filehandle with :raw (the default layer for *nix) or some other appropriate IO Layer in the 1st place.

Sample:

$fh = FileHandle->new($FileName, '>:raw')

Check perldoc open for more details on IO Layers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top