How to check if a file is a text file? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How does Perl know a file is binary?Does Perl6 support something equivalent to Perl5's __DATA__ and __END__ sections?How to get the Terminalsize with perl6/rakudo?file ctime different under perl 5 and perl 6Parsing binary structure with Perl6 GrammarArray vs. list data type?How to make perl6 die on undefined values?Perl6 equivalent of Perl's 'store' or 'use Storable'Defined vs. exists with Perl6 hash keyshow to load Perl5's Data::Printer in Perl6?Reading file line by line in Perl6, how to do idiomatically?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Why do we bend a book to keep it straight?

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Illegal assignment from sObject to Id

How would a mousetrap for use in space work?

How do I find out the mythology and history of my Fortress?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Update module to run alter command

Take 2! Is this homebrew Lady of Pain warlock patron balanced?

What are the out-of-universe reasons for the references to Toby Maguire-era Spider-Man in Into the Spider-Verse?

How do I change colors in Zim (wiki editor) running on Kubuntu 18.10?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

What order were files/directories outputted in dir?

Is it a good idea to use CNN to classify 1D signal?

Do wooden building fires get hotter than 600°C?

Question about debouncing - delay of state change

What was the first language to use conditional keywords?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Performance gap between vector<bool> and array

Why is it faster to reheat something than it is to cook it?

Effects on objects due to a brief relocation of massive amounts of mass

How do living politicians protect their readily obtainable signatures from misuse?

Putting class ranking in CV, but against dept guidelines



How to check if a file is a text file?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How does Perl know a file is binary?Does Perl6 support something equivalent to Perl5's __DATA__ and __END__ sections?How to get the Terminalsize with perl6/rakudo?file ctime different under perl 5 and perl 6Parsing binary structure with Perl6 GrammarArray vs. list data type?How to make perl6 die on undefined values?Perl6 equivalent of Perl's 'store' or 'use Storable'Defined vs. exists with Perl6 hash keyshow to load Perl5's Data::Printer in Perl6?Reading file line by line in Perl6, how to do idiomatically?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








18















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










share|improve this question






























    18















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










    share|improve this question


























      18












      18








      18


      2






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










      share|improve this question
















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







      perl6 file-type






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 15 at 7:05









      Håkon Hægland

      16.7k124394




      16.7k124394










      asked Apr 15 at 6:42









      sid_comsid_com

      9,5831881158




      9,5831881158






















          3 Answers
          3






          active

          oldest

          votes


















          20














          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





          share|improve this answer






























            10














            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)






            share|improve this answer


















            • 2





              actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

              – timotimo
              Apr 15 at 9:02






            • 1





              @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

              – jjmerelo
              Apr 15 at 9:19






            • 1





              @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

              – plugwash
              Apr 15 at 16:43


















            3














            we can make use of the File::Type with the following code.



            use strict;
            use warnings;

            use File::Type;

            my $file = '/path/to/file.ext';
            my $ft = File::Type->new();
            my $file_type = $ft->mime_type($file);

            if ( $file_type eq 'application/octet-stream' )
            # possibly a text file

            elsif ( $file_type eq 'application/zip' )
            # file is a zip archive



            Source: https://metacpan.org/pod/File::Type






            share|improve this answer























            • This is a perl5 module, but question is about a perl6 solution.

              – Valle Lukas
              yesterday











            • This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

              – Brad Gilbert
              11 hours ago











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55683746%2fhow-to-check-if-a-file-is-a-text-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            20














            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





            share|improve this answer



























              20














              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





              share|improve this answer

























                20












                20








                20







                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





                share|improve this answer













                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 15 at 7:48









                Jonathan WorthingtonJonathan Worthington

                10.3k12851




                10.3k12851























                    10














                    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)






                    share|improve this answer


















                    • 2





                      actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                      – timotimo
                      Apr 15 at 9:02






                    • 1





                      @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                      – jjmerelo
                      Apr 15 at 9:19






                    • 1





                      @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                      – plugwash
                      Apr 15 at 16:43















                    10














                    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)






                    share|improve this answer


















                    • 2





                      actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                      – timotimo
                      Apr 15 at 9:02






                    • 1





                      @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                      – jjmerelo
                      Apr 15 at 9:19






                    • 1





                      @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                      – plugwash
                      Apr 15 at 16:43













                    10












                    10








                    10







                    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)






                    share|improve this answer













                    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)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 15 at 6:54









                    jjmerelojjmerelo

                    7,09931851




                    7,09931851







                    • 2





                      actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                      – timotimo
                      Apr 15 at 9:02






                    • 1





                      @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                      – jjmerelo
                      Apr 15 at 9:19






                    • 1





                      @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                      – plugwash
                      Apr 15 at 16:43












                    • 2





                      actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                      – timotimo
                      Apr 15 at 9:02






                    • 1





                      @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                      – jjmerelo
                      Apr 15 at 9:19






                    • 1





                      @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                      – plugwash
                      Apr 15 at 16:43







                    2




                    2





                    actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                    – timotimo
                    Apr 15 at 9:02





                    actually, if the file isn't valid utf8, this will throw an exception. also, it won't understand utf16, for example

                    – timotimo
                    Apr 15 at 9:02




                    1




                    1





                    @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                    – jjmerelo
                    Apr 15 at 9:19





                    @timotimo right, but the original one just checked for ASCII or UTF8. A battery of encodings should have to be checked, but the general idea would be the same.

                    – jjmerelo
                    Apr 15 at 9:19




                    1




                    1





                    @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                    – plugwash
                    Apr 15 at 16:43





                    @jjmerelo Your comment disagrees with the answer to stackoverflow.com/questions/899206/…

                    – plugwash
                    Apr 15 at 16:43











                    3














                    we can make use of the File::Type with the following code.



                    use strict;
                    use warnings;

                    use File::Type;

                    my $file = '/path/to/file.ext';
                    my $ft = File::Type->new();
                    my $file_type = $ft->mime_type($file);

                    if ( $file_type eq 'application/octet-stream' )
                    # possibly a text file

                    elsif ( $file_type eq 'application/zip' )
                    # file is a zip archive



                    Source: https://metacpan.org/pod/File::Type






                    share|improve this answer























                    • This is a perl5 module, but question is about a perl6 solution.

                      – Valle Lukas
                      yesterday











                    • This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                      – Brad Gilbert
                      11 hours ago















                    3














                    we can make use of the File::Type with the following code.



                    use strict;
                    use warnings;

                    use File::Type;

                    my $file = '/path/to/file.ext';
                    my $ft = File::Type->new();
                    my $file_type = $ft->mime_type($file);

                    if ( $file_type eq 'application/octet-stream' )
                    # possibly a text file

                    elsif ( $file_type eq 'application/zip' )
                    # file is a zip archive



                    Source: https://metacpan.org/pod/File::Type






                    share|improve this answer























                    • This is a perl5 module, but question is about a perl6 solution.

                      – Valle Lukas
                      yesterday











                    • This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                      – Brad Gilbert
                      11 hours ago













                    3












                    3








                    3







                    we can make use of the File::Type with the following code.



                    use strict;
                    use warnings;

                    use File::Type;

                    my $file = '/path/to/file.ext';
                    my $ft = File::Type->new();
                    my $file_type = $ft->mime_type($file);

                    if ( $file_type eq 'application/octet-stream' )
                    # possibly a text file

                    elsif ( $file_type eq 'application/zip' )
                    # file is a zip archive



                    Source: https://metacpan.org/pod/File::Type






                    share|improve this answer













                    we can make use of the File::Type with the following code.



                    use strict;
                    use warnings;

                    use File::Type;

                    my $file = '/path/to/file.ext';
                    my $ft = File::Type->new();
                    my $file_type = $ft->mime_type($file);

                    if ( $file_type eq 'application/octet-stream' )
                    # possibly a text file

                    elsif ( $file_type eq 'application/zip' )
                    # file is a zip archive



                    Source: https://metacpan.org/pod/File::Type







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Sandy P. ChaudhrySandy P. Chaudhry

                    986




                    986












                    • This is a perl5 module, but question is about a perl6 solution.

                      – Valle Lukas
                      yesterday











                    • This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                      – Brad Gilbert
                      11 hours ago

















                    • This is a perl5 module, but question is about a perl6 solution.

                      – Valle Lukas
                      yesterday











                    • This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                      – Brad Gilbert
                      11 hours ago
















                    This is a perl5 module, but question is about a perl6 solution.

                    – Valle Lukas
                    yesterday





                    This is a perl5 module, but question is about a perl6 solution.

                    – Valle Lukas
                    yesterday













                    This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                    – Brad Gilbert
                    11 hours ago





                    This could be edited to use File::Type:from<Perl5> and $ft.mime_type($file) to be Perl6 code.

                    – Brad Gilbert
                    11 hours ago

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55683746%2fhow-to-check-if-a-file-is-a-text-file%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Sum ergo cogito? 1 nng

                    419 nièngy_Soadمي 19bal1.5o_g

                    Queiggey Chernihivv 9NnOo i Zw X QqKk LpB