7 FileCache - keep more files open than the system permits
16 The C<cacheout> function will make sure that there's a filehandle open
17 for writing available as the pathname you give it. It automatically
18 closes and re-opens files if you exceed your system file descriptor
23 F<sys/param.h> lies with its C<NOFILE> define on some systems,
24 so you may have to set $FileCache::cacheout_maxopen yourself.
37 # Open in their package.
41 open(*{$pack . '::' . $_[0]}, $_[1]);
46 close(*{$pack . '::' . $_[0]});
49 # But only this sub name is visible to them.
52 $cacheout_numopen = 0;
56 unless (defined $cacheout_maxopen) {
57 if (open(PARAM,'/usr/include/sys/param.h')) {
60 $cacheout_maxopen = $1 - 4
61 if /^\s*#\s*define\s+NOFILE\s+(\d+)/;
65 $cacheout_maxopen = 16 unless $cacheout_maxopen;
67 if (!$isopen{$file}) {
68 if (++$cacheout_numopen > $cacheout_maxopen) {
69 my @lru = sort {$isopen{$a} <=> $isopen{$b};} keys(%isopen);
70 splice(@lru, $cacheout_maxopen / 3);
71 $cacheout_numopen -= @lru;
72 for (@lru) { &cacheout_close($_); delete $isopen{$_}; }
74 cacheout_open($file, ($saw{$file}++ ? '>>' : '>') . $file)
75 or croak("Can't create $file: $!");
77 $isopen{$file} = ++$cacheout_seq;