Doug Hoytehttp://cpanratings.perl.org/dist/Sys-Mmap#11362
Rating: 2 stars
Although this module mostly does what it claims to, it is too low-level to be used reliably from perl. Instead, check out File::Map.
The biggest issue with Sys::Mmap is that it is difficult to ensure that mappings are always unmapped when you are finished with them:
use Sys::Mmap;
eval {
open(my $fh, "/etc/passwd") || die;
mmap(my $str, 0, PROT_READ, MAP_SHARED, $fh) || die;
die "Something bad happened!";
munmap($str);
close($fh);
};
## Mapping still exists (but $fh was closed)
With File::Map your mappings will be unmapped automatically:
use File::Map;
eval {
File::Map::map_file my $str, "/etc/passwd", '<';
die "Something bad happened!";
};
## Mapping has been released
Other issues with Sys::Mmap:
* It is not as portable as File::Map which also supports windows.
* The synopsis seems to be wrong. You have to do "new Sys::Mmap", not "new Mmap". But don't use this interface since it doesn't seem to be possible to clean up mappings after they are no longer needed.
* There is no way to create MAP_FIXED mappings (although you generally don't need this).
Although not an issue with this module, you should be aware that there is a bug in perl versions before 5.17.5 where regular expressions can read one byte past the end of a string. This can segfault your perl process if you create mappings that are exact multiples of your page size (and your OS doesn't create adjacent mappings):
$ uname -a
OpenBSD saturn 5.1 GENERIC#181 amd64
$ perl -E 'say $^V'
v5.12.2
$ perl -MSys::Mmap -e 'open($fh, "/dev/zero"); mmap($str, 4096, PROT_READ, MAP_ANON, $fh); $str =~ /.$/;'
Segmentation fault (core dumped)Doug Hoyteseanohttp://cpanratings.perl.org/dist/Sys-Mmap#3445
Rating: 5 stars
Simple, works as advertised, wish it were core. It's great for mapping in a huge chunk of text and screaming through it with a regex. I haven't benchmarked it, but this is almost certainly much faster than slurping a file when you need read-only access.seano