20

I know you can use the file test operator -B to test if a file is binary, but how does Perl implement this internally?

2 Answers 2

31

From perldoc -f -B:

The -T and -B switches work as follows. The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (>30%) are found, it’s a -B file; otherwise it’s a -T file. Also, any file containing null in the first block is considered a binary file. If -T or -B is used on a filehandle, the current IO buffer is examined rather than the first block. Both -T and -B return true on a null file, or a file at EOF when testing a filehandle. Because you have to read a file to do the -T test, on most occasions you want to use a -f against the file first, as in "next unless -f $file && -T $file".
1
  • 1
    There's an opportunity to use the magic stat cache character "_": "next unless -f $file and -T _" May 22, 2009 at 20:59
11

According to Chapter 11 of the book Learning Perl:

The answer is **Perl cheats**: it opens the file, looks at the first few thousand bytes, and makes an educated guess. If it sees a lot of null bytes, unusual control characters, and bytes with the high bit set, then that looks like a binary file. If there’s not much weird stuff, then it looks like text. It sometimes guesses wrong. If a text file has a lot of Swedish or French words (which may have characters represented with the high bit set, as some ISO-8859-something variant, or perhaps even a Unicode version), it may fool Perl into declaring it binary. So it’s not perfect, but if you need to separate your source code from compiled files, or HTML files from PNGs, these tests should do the trick.
3
  • I don't know if I'd consider that "cheating." It's not like there is really any better way.
    – friedo
    May 23, 2009 at 5:14
  • well since there opening the file , its kind of cheating, dont ya think
    – TStamper
    May 23, 2009 at 8:15
  • 1
    @TStamper What would be a way not considered cheating? It's like having a briefcase and asking if it's full of documents or full of money - you can only answer by looking inside. Aug 6, 2020 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.