CrashBlossom has asked for the wisdom of the Perl Monks concerning the following question:
It attempts to guess whether a file is a text file based on what it sees in the first 4096 characters. I ran across it while seeking an alternative to -T to check for a text file. It seems to work in my tests, but I don't understand why because I don't understand what the regex is matching.sub TextFile { return 0 if (! -f $_[0]); return 0 if (! -r $_[0]); open FH, "<" . $_[0]; my $block = " " x 4096; my $bytesread = sysread FH, $block, 4096; close FH; if (! defined $bytesread) { print "*** ERROR: TextFile: $_[0]: $!\n"; return 0; } return $block =~ /^[\r\n\t -~]*$/s; }
is true if $block does nor include any of \r\n\t -~ before an end of line. But that doesn't make sense. I'm also mystified by the inclusion of the characters -~ in the character class.$block =~ /^[\r\n\t -~]*$/s
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question regarding a regex
by jdporter (Paladin) on Jul 22, 2021 at 19:10 UTC | |
by LanX (Saint) on Jul 22, 2021 at 20:55 UTC | |
|
Re: Question regarding a regex
by hippo (Archbishop) on Jul 22, 2021 at 19:08 UTC | |
|
Re: Question regarding a regex
by eyepopslikeamosquito (Archbishop) on Jul 22, 2021 at 23:21 UTC | |
|
Re: Question regarding a regex
by AnomalousMonk (Archbishop) on Jul 23, 2021 at 00:40 UTC | |
|
Re: Question regarding a regex
by Anonymous Monk on Jul 22, 2021 at 19:26 UTC | |
by AnomalousMonk (Archbishop) on Jul 23, 2021 at 00:05 UTC | |
|
Re: Question regarding a regex
by BillKSmith (Monsignor) on Jul 23, 2021 at 02:31 UTC | |
by afoken (Chancellor) on Jul 23, 2021 at 08:09 UTC | |
by BillKSmith (Monsignor) on Jul 23, 2021 at 13:55 UTC | |
by bliako (Abbot) on Jul 23, 2021 at 08:51 UTC | |
|
Re: Question regarding a regex
by CrashBlossom (Beadle) on Jul 23, 2021 at 21:55 UTC |