File::E...ortable(3)
π File::Edit::Portable π
okay bestie, so the manual name is File::E...ortable(3) (that's just File::Edit::Portable with the middle all squished by the man-page formatter, she's shy like that). Anyway. What she does is: read and write files while keeping the original line-endings intact, no matter the platform. no cap. π¦
β¨ SYNOPSIS β¨
use File::Edit::Portable;
my $rw = File::Edit::Portable->new;
# read a file, replacing original file's line endings with
# that of the local platform's default
my $fh = $rw->read('file.txt');
# edit file in a loop, and re-write it with its original line endings
my $fh = $rw->read('file.txt');
my $wfh = $rw->tempfile;
while (<$fh>){
...
print $wfh $_;
}
$rw->write(contents => $wfh);
# get an array of the file's contents, with line endings stripped off
my @contents = $rw->read('file.txt');
# write out a file using original file's record separator, into a new file,
# preserving the original
$rw->write(contents => \@contents, copy => 'file2.txt');
# replace original file's record separator with a new (custom) one
$rw->write(recsep => "\r\n", contents => \@contents);
# rewrite all files in a directory recursively with local
# platform's default record separator
$rw->dir(dir => '/path/to/files');
# insert new data into a file after a specified line number
$rw->splice(file => 'file.txt', line => $num, insert => \@contents);
# insert new data into a file after a found search term
$rw->splice(file => 'file.txt', find => 'term', insert => \@contents);
# get a file's record separator
$rw->recsep('file.txt'); # string form
$rw->recsep('file.txt', 'hex'); # hex form
$rw->recsep('file.txt', 'type'); # line ending type (nix, win, mac, etc)
πΈ DESCRIPTION πΈ
so like, default perl behaviour is that it reads and writes files using your Operating System's (OS) default record separator β that's the line ending, bestie, the thing at the end of every line. π«Ά and if you open a file on an OS where the record separators came from another OS? things can and do break. it's giving chaos. it's giving "why is my whole file one line".
this module reads in a file, keeps track of that file's current record separators regardless of which OS you're on, and saves them for later writing. π
in scalar context it hands you a file handle whose line endings have been swapped out for your local OS platform's flavour. in list context it hands you an array of the file's contents with the line endings all stripped off. either way, you can mess with that array and send it back in to be written to the same file or a brand new one, and the original file's line endings get re-appended (or a custom ending, if that's the vibe you're going for). β¨
the use case? dynamically reading/writing files while you're on one OS but you literally do not know whether the record separators are platform-standard. shared storage between multiple platforms is the main character scenario here. this module means you don't have to go check every single file by hand, and it's highkey useful when you're looping over a directory where different files may have been written by different platforms. π
π EXPORT π
None by default. See EXPORT_OK.
π EXPORT_OK π
recsep()platform_recsep()
π METHODS π
"new"
Returns a new File::Edit::Portable object. fresh out the box, periodt.
read('file.txt')
In scalar context, will return a read-only file handle to a copy of the file that has had its line endings replaced with those of the local OS platform's record separator. π¦
In list context, will return an array, where each element is a line from the file, with all line endings stripped off.
In both cases, we save the line endings that were found in the original file (which is used when write() is called, by default). π
"write"
Writes the data back to the original file, or alternately a new file. Returns 1 on success. And listen β if you inadvertantly append newlines to the new elements of the contents array, we'll strip them off before appending the real newlines. we got you. π
Parameters:
"file => 'file.txt'"β not needed if you've usedread()to open the file. however, if you have multipleread()s open,write()will die without the'file'param, because it won't know which file you're wanting to write. π"copy => 'file2.txt'"β set this if you want to write to an alternate (new) file, rather than the original."contents => $filehandle"or"contents => \@contents"β Mandatory. either an array with one line per element, or a file handle (file handle is far less memory-intensive). π§"recsep => "\r\n""β Optional. a double-quoted string of any characters you want to write as the line ending (record separator). this value will override what was found in theread()call. common ones are"\r\n"for Windows,"\n"for Unix and"\r"for Mac. use a call toplatform_recsep()as the value to use the local platform's default separator.
"splice"
Inserts new data into a file after a specified line number or search term. βοΈβ¨
Parameters:
"file => 'file.txt'"β Mandatory."insert => \@contents"β Mandatory. an array reference containing the contents to merge into the file."copy => 'file2.txt'"β Optional. we'll read from"file", but we'll write to this new file."line => Integer"β Optional. merge the contents on the line following the one specified here."find => 'search term'"β Optional. merge the contents into the file on the line following the first find of the search term. the search term is put into"qr", so single quotes are recommended, and all regex patterns are honoured. note that we also accept a pre-created"qr//"Regexp object directly (as opposed to a string). π₯"limit => Integer"β Optional. when splicing with the'find'param, set this to the number of finds to insert after. default is stop after the first find. set to0will insert after all finds.
NOTE: Although both are optional, at least one of"line"or"find"must be sent in. If both are sent in, we'll warn, and operate on the line number and skip the find parameter.
Returns an array of the modified file contents. π
"dir"
Rewrites the line endings in some or all files within a directory structure recursively. by default, rewrites all files with the current platform's default line ending. returns an array of the names of the files found. ποΈ
Parameters:
"dir => '/path/to/files'"β Mandatory."types => ['*.txt', '*.dat']"β Optional. specify wildcard combinations for files to work on. we'll accept anything thatFile::Find::Rule::name()method does. if not supplied, we work on all files."maxdepth => Integer"β Optional. specify how many levels of recursion to do after entering the directory. we'll do a full recurse through all sub-directories if this parameter is not set."recsep => "\r\n""β Optional. if this parameter is not sent in, we'll replace the line endings with that of the current platform we're operating on. otherwise, we'll use the double-quoted value sent in."list => 1"β Optional. if set to any true value, we'll return an array of the names of the files found, but won't take any editing action on them. default is disabled.
"recsep('file.txt', $want)"
Returns the record separator found within the file. if the file is empty, we'll return the local platform's default record separator. πΈ
the optional $want parameter can contain either 'hex' or 'type'. if 'hex' is sent in, the record separator will be returned in hex form (eg: "\0d\0a" for Windows). if 'type' is sent in, we'll return a short-form of the line-ending type (eg: win, nix, mac, etc). β
note that this method can be imported into your namespace on demand if you don't need the object functionality of the module. π
platform_recsep($want)
Returns the the current platform's (OS) record separator in string form. (yes bestie, "the the", it's in the original, we don't fix what we don't own π)
the optional $want parameter can contain either 'hex' or 'type'. if 'hex' is sent in, the record separator will be returned in hex form (eg: "\0d\0a" for Windows). if 'type' is sent in, we'll return a short-from of the line-ending type (eg: win, nix, mac, etc). β¨
note that this method can be imported into your namespace on demand if you don't need the object functionality of the module. π
"tempfile"
Returns a file handle in write mode to an empty temp file. perfect little scratchpad, obsessed. π
π AUTHOR π
Steve Bertrand, "<steveb at cpan.org>"
π BUGS π
Please report any bugs or feature requests to <https://github.com/stevieb9/file-edit-portable/issues>
π REPOSITORY π
<https://github.com/stevieb9/file-edit-portable>
π₯ BUILD RESULTS (THIS VERSION) π₯
CPAN Testers: <http://matrix.cpantesters.org/?dist=File-Edit-Portable>
π«Ά SUPPORT π«Ά
You can find documentation for this module with the perldoc command.
perldoc File::Edit::Portable
π LICENSE AND COPYRIGHT π
Copyright 2022 Steve Bertrand.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See <http://dev.perl.org/licenses/> for more information.
and that's a wrap, bestie π
this page came to you courtesy of perl v5.42.0, dated 2025-07-25, and the man-page header was doing that File::E...ortable(3) thing the whole time. slay, go fix some line endings. πβ¨
π¦ where this page comes from
- fedora 43: perl-File-Edit-Portable 1.26-9.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms