__________
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
- Terry Pratchett
I rarely find it a problem. I only have to deal with Unix<->Windows issues, and it's a simple matter to convert between the two. If I forget, the results are usually noticeable enough to remind me right away.
I've used something like this little stub on the off times I've needed to "guess" at filetypes:
sub guess_newline {
my $file = shift;
open my $F, '<', $file or return undef;
my ($buf, $sep);
until ( eof($F) or defined $sep) {
# read a 1-k chunk + a random few bytes
read( $F, $buf, 1024+int(rand(5)) );
# the trailing dot below is important, in case part of a newline is
# truncated in the read!
$sep = $1 if $buf=~m/(\x0A|\x0D|\x0D\x0A)./;
}
close $F;
return $sep;
}
The purpose of the random length change is a workaround for a couple files I've run across where the lines (including newline) were exactly 1024 bytes. As a result, my regex never matched. ;-)
This works very well like this:
{
local $/ = guess_newline($filename) || die "Can't guess sep for $filename";
open my $IN, '<', $filename or die "Can't read $filename: $!";
while (<$IN>) { ... }
}
perlmonks.org content © perlmonks.org and EvanK, fireartist, radiantmatrix, spiritway
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03