first of all, how can you be checking for the line ending of
each line in a loop that relies on the value of that line
ending to break input up line-by-line? if you want to do
this, you can't use
<> until you've determined
what the value of
$/. my advice is don't use
it. just read the data with
read and break
up the lines manually with
split.
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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.