Is this usage of unpack correct if I would like to try this guessing subroutine with the variables first 1000 bytes?
#!/usr/bin/env perl
use warnings;
use 5.10.1;
my $var = ...;
my $part = unpack( 'b1000', $var ) ;
sub is_binary_data {
local $_ = shift;
( tr/ -~//c / length ) >= .3;
}
if ( is_binary_data( $part ) ) {
say "Binary";
}
else {
say "Text";
}
No it isn’t since unpack will create a string of 0 and 1’s (up to 1000 of them) which would certainly pass the ascii test (which I believe
tr, -~,,c / lengthis)I would suggest using just
substr ($var, 0, 1000)instead.Also, maybe
\rand\nshould appear in thetr//.