You clearly want to slurp the whole file
What if the file is hundreds of megs? Here's a little something I whipped up:
use strict; use warnings; my $buf_size = 16_384; open(my $big, shift) or die; open(my $small, shift) or die; my $search_string; { local $/; $search_string = <$small>; } my $buffer = ""; my $pos = 0; while(sysread($big, $buffer, $buf_size, length($buffer) ) ) { if ( (my $index = index($buffer, $search_string)) != -1) { print "FOUND! found the search string at position ", $pos + $ind +ex; exit; } $buffer = substr($buffer, int(length($buffer)/2)); $pos += length($buffer); } print "search string not found";

I'll grant that this also falls prey to the same problem that your has if the smaller file is itself large, but I think that in general this scales much better.

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

f

In reply to Re^2: searching for a binary string in a binary file by thor
in thread searching for a binary string in a binary file by rochlin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.