Question

I have a small routine where I apply some regex over a file and write the whole content to a new file.

perl -p -e "myRegexReplace" %INPUT% >%OUTPUT%

The script is executed on a windows machine.

For testing purpose, I have a file that only contains Unix Linebreaks LF. However, after processing the file the linebreaks are CRLF format.

How can I prevent perl from changing the linefeed format?

Update @ amon:

STDIN :unix
STDIN :crlf
STDOUT :unix
STDOUT :crlf
Was it helpful?

Solution

From the PerlIO-documentation:

:crlf

A layer that implements DOS/Windows like CRLF line endings. On read converts pairs of CR,LF to a single "\n" newline character. On write converts each "\n" to a CR,LF pair. Note that this layer will silently refuse to be pushed on top of itself.

It currently does not mimic MS-DOS as far as treating of Control-Z as being an end-of-file marker.

So on output, the LF gets converted to the unwanted CRLF.

To remove this layer, we want to get the raw filehandle without byte munching. This script demonstrates how to use binmode to change layers:

use feature 'say';

print_layers();

# remove any byte-munching layers (:crlf, :utf8)
# this would also be a nice place to add `:utf8` or some `:encoding(...)`
binmode $_, ":raw" for STDIN, STDOUT, STDERR;

print_layers();

sub print_layers {
  say "== Layers ==";
  for my $fh (STDIN, STDOUT, STDERR) {
    say "$fh :$_" for PerlIO->get_layers($fh);
  }
}

The output should be:

== Layers ==
STDIN :unix
STDIN :crlf
STDOUT :unix
STDOUT :crlf
STDERR :unix
STDERR :crlf
== Layers ==
STDIN :unix
STDOUT :unix
STDERR :unix

I couldn't quite figure out how to convince the open pragma to do this for me.

OTHER TIPS

simply piping to tr:

perl -p -e "myRegexReplace" %INPUT% | tr -d \r >%OUTPUT%

tr download: GNUWin32, UnxUtils Updates

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