Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Dealing with files with differing line endings

by stevieb (Canon)
on Nov 05, 2021 at 21:17 UTC ( #11138487=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2022-01-22 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?