- 1 year ago
Perl writes LF as CRLF on Windows
When writing files in Perl on Windows, the default behavior is to use the CRLF (Carriage Return + Line Feed) line endings, which is the standard line ending format for text files on Windows. However, if you want to write LF (Line Feed) line endings instead of CRLF, you can use the binmode
function with the :raw
or :crlf
Perl layers. Here's an example:
perluse strict;
use warnings;
my $filename = 'example.txt';
# Open the file in binary mode with :raw layer
open(my $fh, '>:raw', $filename) or die "Cannot open file: $!";
# Write LF line endings
print $fh "Line 1\n";
print $fh "Line 2\n";
print $fh "Line 3\n";
# Close the file
close($fh);
In this example, the open
function is used with the :raw
layer to open the file in binary mode, which disables automatic newline conversion. Then, LF line endings are explicitly used when writing to the file.
Alternatively, you can use the :crlf
layer with open
to specify that you want CRLF line endings:
perlopen(my $fh, '>:crlf', $filename) or die "Cannot open file: $!";
By using either :raw
or :crlf
as the Perl layer in the open
function, you can control the line ending format of the file being written.