in reply to Re^2: Possible to have regexes act on file directly (not in memory)
in thread Possible to have regexes act on file directly (not in memory)

From a cursory glance at the documentation, no. That's still treating that memory map as a filehandle. You need to treat it as a string or scalar. A quick check on cpan gives Sys::Mmap as a reasonable candidate based on its documentation.

  • Comment on Re^3: Possible to have regexes act on file directly (not in memory)

Replies are listed 'Best First'.
Re^4: Possible to have regexes act on file directly (not in memory)
by davido (Cardinal) on May 03, 2014 at 05:33 UTC

    The winner seems to be File::Map. It is less complicated to use, and at least performs in correct accordance to its documentation (unlike Sys::Mmap). ++ to leont once again.

    use strict; use warnings; use File::Map 'map_file'; map_file my $map, '/usr/share/dict/words', '<'; while( $map =~ m/^(wal.*)$/mg ) { print "$1\n"; }

    Though its user interface is fairly simple, it is a module where reading 100% of the documentation is probably a very good idea.


    Dave

Re^4: Possible to have regexes act on file directly (not in memory)
by davido (Cardinal) on May 03, 2014 at 05:21 UTC

    In principle this seems to work. Here's an example:

    use strict; use warnings; use Sys::Mmap; my $string; open my $fh, '<', '/usr/share/dict/words' or die $!; Sys::Mmap::mmap( $string, 0, MAP_SHARED, PROT_READ, $fh ); while( $string =~ m/^(wal.*)$/mg ) { print $1, "\n"; } Sys::Mmap::munmap( $string ) or die "munmap: $!"; close $fh;

    On my system I get a dump of everything in the dict/words file starting with 'wal'. However, I end up getting the following error message when munmap is called:

    variable is not a string at ./mytest.pl line 17.

    The XS code testing whether the variable being unmapped is a string is this:

    if(SvTYPE(var) != SVt_PV) { croak("variable is not a string"); return; }

    ...which doesn't strike me as wrong. Must be something in the XS for 'mmap' that is not happening properly.


    Dave