I'd sure like to know how Perl determines ("heuristic guess" says perldoc!) that the file is or is not a binary so that I can do that in Ada.

The method used is described here (perldoc functions -X):

The -T and -B tests work as follows. The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If so, it's a -T file. Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a -B file; otherwise it's a -T file. Also, any file containing a zero byte in the examined portion is considered a binary file. (If executed within the scope of a use locale which includes LC_CTYPE, odd characters are anything that isn't a printable nor space in the current locale.) 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 an empty 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.

To see the gory details of the implementation, search for pp_fttext in pp_sys.c in the Perl source code (which is also used for pp_ftbinary).

Command Line Examples

As you can see from the bash command line examples below, the Linux file command is much more complex and sophisticated in that it recognizes a number of specific file formats ... while perl's -B file test just crudely guesses whether a file is binary or text based on a simple heuristic:

$ perl -e '-B q{perl-5.38.0.tar.gz} and warn "binary file\n"' binary file $ file perl-5.38.0.tar.gz perl-5.38.0.tar.gz: gzip compressed data, last modified: Sun Jul 2 22 +:26:17 2023, max compression, from Unix, original size modulo 2^32 97 +505280 $ perl -e '-B q{/usr/bin/perl} and warn "binary file\n"' binary file $ file /usr/bin/perl /usr/bin/perl: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV) +, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildI +D[sha1]=6ecb6a6b0956a41de73c75facebcb500b525b860, for GNU/Linux 3.2.0 +, stripped

See Also

Updated: Added "Command Line Examples" and "See Also" sections.


In reply to Re^4: How to use the -T file op as a one-liner by eyepopslikeamosquito
in thread How to use the -T file op as a one-liner by kwolcott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.