As others have pointed out, lines end differently on UNIX and Windows. What you want to do is ensure that your input record separator (stored in the Perl special variable <c>$/<c/> {see perlvar}) is set correctly for the type of file you wish to read.
local $/ = "\r\n"; # read with Windows-style endings while (<FH>) { print "Found: $_"; }
(Quick note: the line endings stay on the line unless you chomp them off.)
There are other approaches if you don't know what line endings you will be dealing with. The easiest is to slurp the whole file and then split it yourself, though this is a bad idea for very large files.
use File::Slurp; my $content = read_file('my_file.txt'); for ( split(/[\r\n]+/, $content) ) { print "Found: $_\n"; }
This splits on any of \r, \n, or \r\n (or, incidentally, \n\r), taking care of Mac, Windows, or UNIX line endings. As a side effect, blank lines are skipped and all line endings are trimmed off. This may or may not be desirable.
In reply to Re: File lines being treated as one.
by radiantmatrix
in thread File lines being treated as one.
by minixman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |