derby points out the answer to your question, but there's another couple of pieces.
- If you want to process the file one line at a time, you need to set $/ anyway, or you may not be reading one line at a time.
- If you need to preserve the original line-endings for a write-out operation at some point, you can just easily modify the sub to set $\ as well.
Because of the first, the whole structure is kind of odd anyway, since with Mac line endings, you'd slurp the whole file to find out that you have those line endings.
Better to do:
open IN, '<', $filename or die ("Can't open $filename: $!");
sysseek IN, -5, 2;
my $last_five;
sysread IN, $last_five, 5;
## find out what the EOL chars are and set $\ to match
$/ = $1 if $last_five =~ m/(\r{0,1}\n)$/s;
sysseek IN,0,0;
while (<IN>) {
chomp;
# now process stuff #
}
This is predicated on the text-files being well-formed (ending with an EOL before EOF), so you may need to handle the possibility of malformed files or whatnot.
<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law
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.