How to check if a file is a text file?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?

Understanding Cursive /Joined Writing in Irish Register Death

Are Democrats more likely to believe Astrology is a science?

Is a suit against a Univeristy Dorm for changing policies on a whim likely to succeed (USA)?

Do they still use tiger roars in the 2019 "Lion King" movie?

How can I locate a missing person abroad?

Is there an inconsistency about Natasha Romanoff's middle name in the MCU?

What was the relationship between Einstein and Minkowski?

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

Are scroll bars dead in 2019?

How do EVA suits manage water excretion?

Might have gotten a coworker sick, should I address this?

Why is Kirchoff's loop rule true in a DC circuit?

Can the card disintegrate destroy creatures with indestructible?

What officially disallows US presidents from driving?

How are aircraft depainted?

Evidence that matrix multiplication cannot be done in O(n^2 poly(log(n))) time

Should I leave the first authorship of our paper to the student who did the project whereas I solved it?

How to work with a technician hired with a grant who argues everything

Where can I get an anonymous Rav Kav card issued?

Have there been any countries that voted themselves out of existence?

What are uses of the byte after BRK instruction on 6502?

Sol Ⅲ = Earth: What is the origin of this planetary naming scheme?

What does a Light weapon mean mechanically?

Why is the Digital 0 not 0V in computer systems?



How to check if a file is a text file?


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 margin-bottom:0;








22















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










share|improve this question
































    22















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










    share|improve this question




























      22












      22








      22


      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

      19.9k13 gold badges47 silver badges105 bronze badges




      19.9k13 gold badges47 silver badges105 bronze badges










      asked Apr 15 at 6:42









      sid_comsid_com

      10.1k19 gold badges81 silver badges161 bronze badges




      10.1k19 gold badges81 silver badges161 bronze badges

























          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


















            4
















            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
              Apr 17 at 16:17






            • 2





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

              – Brad Gilbert
              Apr 18 at 19:30













            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/4.0/"u003ecc by-sa 4.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

                12.9k1 gold badge39 silver badges67 bronze badges




                12.9k1 gold badge39 silver badges67 bronze badges


























                    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

                    8,6624 gold badges22 silver badges54 bronze badges




                    8,6624 gold badges22 silver badges54 bronze badges










                    • 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











                    4
















                    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
                      Apr 17 at 16:17






                    • 2





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

                      – Brad Gilbert
                      Apr 18 at 19:30















                    4
















                    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
                      Apr 17 at 16:17






                    • 2





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

                      – Brad Gilbert
                      Apr 18 at 19:30













                    4














                    4










                    4









                    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 Apr 17 at 6:48









                    Sandy P. ChaudhrySandy P. Chaudhry

                    1086 bronze badges




                    1086 bronze badges















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

                      – Valle Lukas
                      Apr 17 at 16:17






                    • 2





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

                      – Brad Gilbert
                      Apr 18 at 19:30

















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

                      – Valle Lukas
                      Apr 17 at 16:17






                    • 2





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

                      – Brad Gilbert
                      Apr 18 at 19:30
















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

                    – Valle Lukas
                    Apr 17 at 16:17





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

                    – Valle Lukas
                    Apr 17 at 16:17




                    2




                    2





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

                    – Brad Gilbert
                    Apr 18 at 19:30





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

                    – Brad Gilbert
                    Apr 18 at 19:30


















                    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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?