|more useful options
Dealing with files with differing line endingsby dd-b (Monk)
|on Nov 05, 2021 at 19:35 UTC
dd-b has asked for the wisdom of the Perl Monks concerning the following question:
Because of multiple systems (with different OSs) sharing files on a file-server, I find my code running into "text files" with different line endings from the system-default for the system they are running on. I can't predict the line endings in advance (can't deduce it reliably from filename, extension, directory, or whatever).
The files are all small by today's standards, thousands of characters not billions, and in these applications I'm not worried they'll ever challenge memory size (so the idea below, which slurps the whole file first, doesn't look risky for this particular application).
Since $/ is a simple string (not a REGEXP), I can't just put a suitable REGEXP there and get the file split into lines on any standard terminator. (I've got DOS, FreeBSD and Unix, and possibly MAC interpretations of text files to cope with.)
Is there a consensus best-practice for this?
I'm currently thinking of slurping the whole file and then breaking it down in my code. If there isn't already a module that handles this cleanly?