tags:

views:

279

answers:

2

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

+17  A: 
chuck
There's an opportunity to use the magic stat cache character "_": "next unless -f $file and -T _"
glenn jackman
+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
I don't know if I'd consider that "cheating." It's not like there is really any better way.
friedo
well since there opening the file , its kind of cheating, dont ya think
TStamper