Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I've found something more like the following to be useful:

                    my $max_len = max(8096,map{length} @strings);
                    my $regexp = "(" . join("|", map {quotemeta} @strings) . ")";
                    my @buf = ('');
                    while (1) {
                            my $len =
  • I hope you're using this code only as part of the mainline of a script and not as a subroutine or in a module. But even still, it's often the case that scripts turn into modules at some point, so don't use /o if you can help it. It doesn't really buy you anything and could actually cause problems as your program grows. If you think you need /o, you really just need qr//. When someone uses /o it is almost always a case of premature optimization (99.99999999999% of the time :-).
  • It’s easy to arrange:

    my $max_len = max map length, @strings;
    my $rx = qr/(@{[ join '|', map quotemeta, @strings ]})/;
    my $buf = '';
    while ( read $fh, $buf, 8096, length $buf ) {
        return $1 if $buf =~ $rx;
        substr $buf, 0, length $buf - $max_len, '';

    As a bonus the code is shorter and clearer.

    The following is a tweak to avoid unnecessarily shrinking $buf when the space is needed for the read that immediately follows, and it may or may not be faster by a few percent. I didn