Update: Wanted to mention that this will not work if the pattern crosses 2 or more chunks.

The following uses MCE which does not read the entire file into memory. The example is based on fastsearch.pl from File::Map.

use 5.010; use strict; use warnings; use MCE::Flow; die "Not enough arguments given\n" if @ARGV < 2; my $regex = shift; $regex = qr/$regex/; sub user_func { my ($mce, $slurp_ref, $chunk_id) = @_; if (my $match = ${ $slurp_ref } =~ $regex ? 1 : 0) { $mce->gather($match); $mce->abort; } } for my $filename (@ARGV) { my @match = mce_flow_f { use_slurpio => 1 }, \&user_func, $filename; say "File '$filename' does".( scalar @match ? "" : "n't" )." match"; }

Am providing a comparison in the event performance is a concern. Basically, MCE keeps up with File::Map.

$ time ./file_map.pl patternabc big_file File 'big_file' doesn't match real 0m0.327s user 0m0.267s sys 0m0.060s $ time ./mce_slurp.pl patternabc big_file File 'big_file' doesn't match real 0m0.152s user 0m0.263s sys 0m0.118s

Also, see Re: Threads From Hell #2: How To Parse A Very Huge File.


In reply to Re: Possible to have regexes act on file directly (not in memory) by marioroy
in thread Possible to have regexes act on file directly (not in memory) by Nocturnus

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.