in reply to How reliable is -T as a test for ASCII files?

From perldoc -f -X :

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 stdio 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".

Replies are listed 'Best First'.
Re: How reliable is -T as a test for ASCII files?
by cLive ;-) (Prior) on May 12, 2004 at 23:17 UTC

    "next unless -f $file && -T $file"

    That's a little bit tautologous. If it's not a file, it's never going to be a text file :) And if it is a text file, my guess is that it's also a file :)

    The only question with file testing (when not running as root) is whether you have permission to test the file. We run some file tests through sudo on web servers when testing user's files - otherwise you get unexpected results when testing files with permissions 600 / 700 etx.

    cLive ;-)