Yes it's been yonks since I posted any perl. Well today I learned that read() can take an offset to where to put data in your $buf, so I can implement what should be an efficient grep for multiple strings in binary data (i.e. where I can't do: while (<$fh>) ). So given $fh and @strings and max() from List::Util, I can do this:
my $max_len = max(map{length} @strings);
my $regexp = "(" . join("|", map {quotemeta} @strings) . ")";
my $buf = '';
while (1) {
substr($buf, 0, length($buf) - $max_len) = "";
my $len = read($fh, $buf, 8096, length($buf));
last unless $len;
if ($buf =~/$regexp/o) {
return $1;
}
}
I could probably add code to show where in the file it matched, but I don't need that.
Use array storage instead? (Score:1)
my $max_len = max(8096,map{length} @strings);
my $regexp = "(" . join("|", map {quotemeta} @strings) . ")";
my @buf = ('');
while (1) {
my $len =
Re:Use array storage instead? (Score:2)
/o is *evil* (Score:1)
Re:/o is *evil* (Score:2)
Use a meaningful loop termination condition (Score:1)
It’s easy to arrange:
As a bonus the code is shorter and clearer.
The following is a tweak to avoid unnecessarily shrinking
$bufwhen the space is needed for thereadthat immediately follows, and it may or may not be faster by a few percent. I didn