I want to use binmode on binary files in Perl. So how can I know the file at hand is binary or text?
That means first I’d read full file to know if it is in binary or text then rewind the FP with binmode set if it is binary.
Since Perl -B/-T checks only a beginning block of text, it usually classifies pdf as text file(the source code could be hiding in any extension like .gif,.pdf etc). So I’d need to read full file data to decide if it is binary or text file.
I’ve heard that any source code file( .pl,.c,.php etc) will not contain invisible characters like 0x0-0x1f and 0xff onwards.
Can I check every byte to be in this range to declare whether or not it is a source code file?
Do you really need to read the file in text mode if it’s a text file? You could use
binmodeunconditionally.If you have a piece of code that deal with text files, it can filter out any extraneous carriage returns (0D). If you don’t have a such a piece of code, then it surely doesn’t matter if the carriage returns are left in.