[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: mmap was Re: [FWP] rewrite and simplify (out of memory)



Quoting John Carter (john@dwaf-hri.pwv.gov.za):
> It strikes me that there should be a fun way to mmap the file.

Yeah, something like:

use Mmap;
use Term::ReadLine;

$file=new Term::ReadLine("cat")->readline("Please select a file to view: ");

open(FILE, $file) || die "Can't open $file for reading ($!)\n";
mmap($newsgroups, 0, PROT_READ, MAP_SHARED, FILE)
   || die "Can't mmap $file ($!)\n";
syswrite(STDOUT, $newsgroups, length($newsgroups))
   || die "syswrite() failed ($!)\n";
munmap($newsgroups) || die "munmap() failed ($!)\n";


This requires the Mmap module from CPAN, of course. It's potentially quite
an efficient way of doing it (depending on how well mmap() is implemented on
your operating system). mmap() is not that useful in Perl, as there are too
many ways you can hang yourself with it, and Perl handles large files fairly
well without it. It can simplify coding in the case of large files that
can't be chunked by $/.

Conclusion: mmap() is Fun In C, but not Fun With Perl.

-- 
Adam Rice -- wysiwyg@glympton.airtime.co.uk -- Blackburn, Lancashire, England

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org