简体   繁体   English

使用Perl Win7为Linux编写一个文件,只有Linux行结尾

[英]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. 这个Perl脚本在Win7运行 ,修改了将在Linux机器上读取的Clearcase配置规范。 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) Clearcase非常挑剔它的行结尾,它们必须精确且只有\\ n(0x0A)然而尽量我不能让Perl只吐出\\ n结尾,它们通常会出现\\ 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. 这是Perl片段,运行在一系列配置规范元素上,并将element /somevob/...位转换为element /vobs/somevob/...并打印到文件句柄。

        $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 有时,数组中的元素是多行的,并以\\ 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. 当我在二进制查看器中查看Win7上编写的文件时,总会有一个0x0D 0x0A换行序列,Linux上的Clearcase抱怨。 This appears to come from the print. 这似乎来自印刷品。

Any suggestions? 有什么建议么? I thought this would be a 10 minute job... 我以为这将是一个10分钟的工作......

Try 尝试

$fh->binmode;

Otherwise you're probably in text mode, and for Windows this means that \\n is translated to \\r\\n . 否则你可能处于文本模式,对于Windows,这意味着\\n被翻译为\\r\\n

You are running afoul of the :crlf IO Layer that is the default for Perl on Windows. 您正在运行:crlf IO Layer,这是Windows上Perl的默认设置。 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. 您可以在事后使用binmode删除此图层,或者您可以使用:raw (* nix的默认图层)或第一个位置的其他适当IO图层打开文件句柄。

Sample: 样品:

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

Check perldoc open for more details on IO Layers. 检查perldoc open以获取有关IO层的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM