kwolcott has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to use the Perl -T file operator as a one-liner. How to do that?
  • Comment on How to use the -T file op as a one-liner

Replies are listed 'Best First'.
Re: How to use the -T file op as a one-liner
by kcott (Archbishop) on Aug 13, 2023 at 06:14 UTC

    G'day kwolcott,

    The documentation for that can be a little difficult to find. It's under "-X functions".

    — Ken

      You guys just made my day: Ken (kcott) answers Ken (kwolcott) ... and both with "cott" in their surnames! :)

        Ken (kcott) answers Ken (kwolcott)

        I too noticed that before parsing the question...
        For a moment I thought kcott was using a pseudonym and talking to himself...

Re: How to use the -T file op as a one-liner
by eyepopslikeamosquito (Archbishop) on Aug 13, 2023 at 09:34 UTC
Re: How to use the -T file op as a one-liner
by kwolcott (Acolyte) on Aug 13, 2023 at 06:58 UTC
    I have figured out the following (I'm sure there is a much better way!): /opt/local/bin/perl -e 'use strict; use warnings; use v5.34.1; say "binary" if (-B "/opt/local/bin/perl")' Thanks, Ken Wolcott

      Nice work Ken number one!

      Coincidentally, I just finished watching the excellent What's new in perl 5.38 by Paul "Leonerd" Evans, delivered at TPRC 2023, which taught me that:

      • use v5.36 implies use strict and use warnings

      ... so by upgrading to perl 5.36 or higher you can lose the verbose use strict; use warnings; from your excellent one-liner.

      Update: of course, use v5.12 or higher already implies use strict, so you don't need that in your original v5.34 one liner. See also: the clever perle alias invented and heavily used by Ken number two.

        Of course, the real creativity comes in when you manage to incorporate those warnings into your one-liner as part of the input to generate your output. But that probably would require posting the solution in Obfuscated Code ;-)

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

      If that's all you need it for, I'd probably use the shorter, and more informative, file command (available on every Unix and Unix-like system that I've used in the last 40 years) rather than a Perl one-liner.

      $ file /usr/bin/perl /usr/bin/perl: PE32+ executable (console) x86-64 (stripped to external + PDB), for MS Windows, 11 sections $ file /usr/bin/perl > file_perl $ file file_perl file_perl: ASCII text $ perle 'say -T "/usr/bin/perl" ? "text" : "binary"' binary $ perle 'say -T "file_perl" ? "text" : "binary"' text

      In case you haven't yet picked up on ++eyepopslikeamosquito's reference:

      $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'

      — Ken

        Thanks to all of you for the responses. I'm trying to re-learn, eventually master, programming in Ada. As simple learning exercises, I'm trying to perform **minimal** emulation of several UNIX/Linux tools/utilities (such as grep, cat, head, wc, etc) using Ada. I'd like to avoid disabling my terminal by using any of these emulations on a "binary" file, so I'd like to use "file" or "perl -T" in Ada via a system call. I'm not good enough at Ada at this point to enable my own "-T" file operator, but that's one of the many things I'm aiming for. 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. I like the warning I get when using "less" with a file with binary content; it warns me! How does it determine that? The problem I still have with the Perl one-liner is that I don't know how to embed the filename I want to query inside double quotes or how to escape the slash path separators so that it is not interpreted as a regex. The reason why I went with the "perl -T" route over the "file" route was that I wanted a simple "text" or "binary" response, nothing I needed to parse further. Thanks, Ken Wolcott