I know you can use the file test operator -B to test if a file is binary, but how does Perl implement this internally?
There's an opportunity to use the magic stat cache character "_": "next unless -f $file and -T _"
glenn jackman
2009-05-22 20:59:22
+5
A:
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.
TStamper
2009-05-22 18:20:00