sid_com
sid_com

Reputation: 25137

First 1000 bytes of a variable

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";
}

Upvotes: 1

Views: 155

Answers (1)

Dallaylaen
Dallaylaen

Reputation: 5318

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 / length is)

I would suggest using just substr ($var, 0, 1000) instead.

Also, maybe \r and \n should appear in the tr//.

Upvotes: 4

Related Questions