in reply to How to use the -T file op as a one-liner
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to use the -T file op as a one-liner
by eyepopslikeamosquito (Archbishop) on Aug 13, 2023 at 07:44 UTC | |
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:
... 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. | [reply] [d/l] [select] |
by cavac (Prior) on Aug 13, 2023 at 11:04 UTC | |
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
| [reply] |
Re^2: How to use the -T file op as a one-liner
by kcott (Archbishop) on Aug 13, 2023 at 14:29 UTC | |
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.
In case you haven't yet picked up on ++eyepopslikeamosquito's reference:
— Ken | [reply] [d/l] [select] |
by kwolcott (Acolyte) on Aug 13, 2023 at 21:53 UTC | |
| [reply] |
by kcott (Archbishop) on Aug 14, 2023 at 00:57 UTC | |
"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. You can just let Perl handle the embedding of filenames/pathnames for you; e.g.
Note that the commands are identical throughout. You can supply a filename or an absolute/relative pathname in whatever format you want (as an argument to that command). In case you were wondering, file_perl still exists from earlier examples:
— Ken | [reply] [d/l] [select] |
by eyepopslikeamosquito (Archbishop) on Aug 14, 2023 at 02:36 UTC | |
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:
See Also
Updated: Added "Command Line Examples" and "See Also" sections. | [reply] [d/l] [select] |