We don't bite newbies here... much | |
PerlMonks |
Edit files automatically cross-platform, line-endings unknownby stevieb (Canon) |
on Sep 30, 2015 at 20:56 UTC ( [id://1143468]=CUFP: print w/replies, xml ) | Need Help?? |
I had a need for a piece of my software to be platform independent when reading/writing files with any type of line endings. So, I'm sure there are better/more efficient ways to do this, but I couldn't find one and really wanted to better understand record separators, so I wrote File::Edit::Portable. This module will read in a file on any platform, save the existing record separators (line endings), and modify them to the local platforms endings for use. It'll return either a file handle or an array of the file contents. You can optionally then push back an array of contents, and the module will rewrite the file (or optionally a new one), using the line endings that were found while reading (or optionally a user supplied one). This means it'll open Unix files on Windows and rewrite the file with Unix endings, and vice-versa on Windows and Mac (the three I've tested).
Output:
We've also got a non-OO interface:
I definitely know it's quite inefficient, but it's my first working prototype. There are a lot of things missing (unicode and open filters for instance). Criticisms or existing ways to do this are very welcome. -stevieb
Back to
Cool Uses for Perl
|
|