in reply to mac win and unix EOL chars, $/ question
there is probably already a module that does the following, but this code reads in a file in chunks of a specified number of bytes and splits the input into an array containing lines and line endings in the order they are found in the file. so, passing the array returned to print should print out the exact, original file contents.
$CHUNK_SIZE = 4096; open FILE, $file or die "$!\r\n"; while (read FILE, $chunk, $CHUNK_SIZE) { # split the chunk into a list of parts, keeping # the line endings in the array @parts = split /(\r\n?|\n)/, $chunk; if (defined $partial) { $part[0] = $partial . $part[0]; undef $partial; } # if the last part is not a line ending, then # the line could potentially be continued in # the following chunk if ($parts[-1] !~ /^\r\n?|\n$/) { $partial = pop @parts; } push @lines_and_endings, @parts; } push @lines_and_endings, $partial if defined $partial; close FILE; print @lines_and_endings; @just_lines = grep { ! /^\r\n?|\n$/ } @lines_and_endings;
|
|---|