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;
Does Perl6 have something like the Perl5 -T file test to tell if a file is a text file?
perl6 file-type
add a comment
|
Does Perl6 have something like the Perl5 -T file test to tell if a file is a text file?
perl6 file-type
add a comment
|
Does Perl6 have something like the Perl5 -T file test to tell if a file is a text file?
perl6 file-type
Does Perl6 have something like the Perl5 -T file test to tell if a file is a text file?
perl6 file-type
perl6 file-type
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
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
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
add a comment
|
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)
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
add a comment
|
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
This is a perl5 module, but question is about a perl6 solution.
– Valle Lukas
Apr 17 at 16:17
2
This could be edited touse File::Type:from<Perl5>
and$ft.mime_type($file)
to be Perl6 code.
– Brad Gilbert
Apr 18 at 19:30
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment
|
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
add a comment
|
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
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
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
add a comment
|
add a comment
|
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)
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
add a comment
|
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)
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
add a comment
|
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)
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)
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
add a comment
|
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
add a comment
|
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
This is a perl5 module, but question is about a perl6 solution.
– Valle Lukas
Apr 17 at 16:17
2
This could be edited touse File::Type:from<Perl5>
and$ft.mime_type($file)
to be Perl6 code.
– Brad Gilbert
Apr 18 at 19:30
add a comment
|
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
This is a perl5 module, but question is about a perl6 solution.
– Valle Lukas
Apr 17 at 16:17
2
This could be edited touse File::Type:from<Perl5>
and$ft.mime_type($file)
to be Perl6 code.
– Brad Gilbert
Apr 18 at 19:30
add a comment
|
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
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
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 touse File::Type:from<Perl5>
and$ft.mime_type($file)
to be Perl6 code.
– Brad Gilbert
Apr 18 at 19:30
add a comment
|
This is a perl5 module, but question is about a perl6 solution.
– Valle Lukas
Apr 17 at 16:17
2
This could be edited touse 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
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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