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