bec40bde62beb5077a26e1a7b41539e2b8301653
[p5sagit/p5-mst-13.2.git] / lib / cacheout.pl
1 #!/usr/bin/perl
2
3 # Open in their package.
4
5 sub cacheout'open {
6     open($_[0], $_[1]);
7 }
8
9 # But only this sub name is visible to them.
10
11 sub cacheout {
12     package cacheout;
13
14     ($file) = @_;
15     if (!$isopen{$file}) {
16         if (++$numopen > $maxopen) {
17             local(@lru) = sort {$isopen{$a} <=> $isopen{$b};} keys(%isopen);
18             splice(@lru, $maxopen / 3);
19             $numopen -= @lru;
20             for (@lru) { close $_; delete $isopen{$_}; }
21         }
22         &open($file, ($saw{$file}++ ? '>>' : '>') . $file)
23             || die "Can't create $file: $!\n";
24     }
25     $isopen{$file} = ++$seq;
26 }
27
28 package cacheout;
29
30 $seq = 0;
31 $numopen = 0;
32
33 if (open(PARAM,'/usr/include/sys/param.h')) {
34     local($.);
35     while (<PARAM>) {
36         $maxopen = $1 - 4 if /^\s*#\s*define\s+NOFILE\s+(\d+)/;
37     }
38     close PARAM;
39 }
40 $maxopen = 16 unless $maxopen;
41
42 1;