http://qs1969.pair.com?node_id=11138487


in reply to Dealing with files with differing line endings

My File::Edit::Portable was written to deal with this exact situation.

Get a file handle of the file with the record separators changed to that of the local platform, make changes, and write it back to the same file with the original record separators:

use File::Edit::Portable; my $rw = File::Edit::Portable->new; my $fh = $rw->read('file.txt'); ... $rw->write(contents => $fh);

Get an array of a file's contents with the line endings stripped off (one line per element), make changes, and write the data back to the original file (the original line endings will be preserved and put back into place automagically):

my @contents = $rw->read('file.txt'); for (@contents) { ... } $rw->write(contents => \@contents);

There's a myriad of other magic you can do as well, like automatically making a backup copy of each file, chaging line endings, using custom line endings, checking what endings a file is using, splicing stuff into the files etc.

Replies are listed 'Best First'.
Re^2: Dealing with files with differing line endings
by dd-b (Monk) on Nov 05, 2021 at 21:21 UTC
    Was guessing I wasn't the first person to have something like this problem! Thanks for pointing out your module.