I am trying to read in the contents of a file, so that I can commit it to a database (I dont need to parse the data and don't really care what it says).
The files can be anything any arbitry type (.exe, .txt, no extention, etc.), but from what I can tell, there is a non-binary way to read a file, and a specific binary way (using binmode), and it seems that开发者_运维百科 if you use them inappropriately for the wrong type, it can mangle your files (at least on windows?)
Is there a slick way of reading in the contents of a file without having to worry about what kind of data it has?
After opening a filehandle in read
mode, apply binmode
to it.
open my $fh, '<', '/path/to/file' or die "Error while opening: $!";
binmode $fh;
Alan's answer is correct. Even if the file is "text" you want to treat is as binary like any other file. It will not "mangle" your file.
However, when it comes time to deal with your text file as a text file (display, processing, etc) you might need to do s/\x0D?\x0A/\n/g; s/\x0D/\n/g;
on it. This will turn Mac, Windows and Unix newlines into the current system's newlines.
If you could give more context as to what you are doing, maybe you could get a better answer.
精彩评论