Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file is a text file?

Tags:

Does Perl6 have something like the Perl5 -T file test to tell if a file is a text file?

like image 622
sid_com Avatar asked Apr 15 '19 06:04

sid_com


2 Answers

There's nothing built in, however there is a module Data::TextOrBinary that does that.

use Data::TextOrBinary; say is-text('/bin/bash'.IO);                            # False say is-text('/usr/share/dict/words'.IO);                # True 
like image 122
Jonathan Worthington Avatar answered Oct 14 '22 02:10

Jonathan Worthington


That's a heuristic that has not been translated to Perl 6. You can simply read it in UTF8 (or ASCII) to do the same:

given slurp("read-utf8.p6", enc => 'utf8') -> $f {     say "UTF8"; } 

(substitute read-utf8.p6 by the name of the file you want to check)

like image 42
jjmerelo Avatar answered Oct 14 '22 02:10

jjmerelo