in reply to Process file regardless of the platform it was created on
If you never encounter any really huge CR-format files, you might want something like this:
Either that or else you just use slurp mode in all cases, and split into lines (using /[\r\n]+/) if you really need to do that.while (<>) { my @lines; if ( /\n$/ ) { tr/\r\n//d; @lines = ( $_ ); } else { @lines = split /\r/; } for my $line ( @lines ) { # do stuff with each line of text } }
If you have to worry about getting really huge files in any of the three possible formats, you'll want to diagnose each file first -- at least, check the file size first, and if it's really big, read just enough (e.g. read FH, $_, 2048;), to figure out what the line termination is, set $/ accordingly, then rewind it (seek FH,0,0;) to read the whole thing as it was intended to be read.
|
|---|