xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/cacheout.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#
2*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
3*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
6*0Sstevel@tonic-gate# programming techniques.
7*0Sstevel@tonic-gate#
8*0Sstevel@tonic-gate# Suggested alternative: FileCache
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate# Open in their package.
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gatesub cacheout'open {
13*0Sstevel@tonic-gate    open($_[0], $_[1]);
14*0Sstevel@tonic-gate}
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate# Close as well
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gatesub cacheout'close {
19*0Sstevel@tonic-gate    close($_[0]);
20*0Sstevel@tonic-gate}
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate# But only this sub name is visible to them.
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gatesub cacheout {
25*0Sstevel@tonic-gate    package cacheout;
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate    ($file) = @_;
28*0Sstevel@tonic-gate    if (!$isopen{$file}) {
29*0Sstevel@tonic-gate	if (++$numopen > $maxopen) {
30*0Sstevel@tonic-gate	    local(@lru) = sort {$isopen{$a} <=> $isopen{$b};} keys(%isopen);
31*0Sstevel@tonic-gate	    splice(@lru, $maxopen / 3);
32*0Sstevel@tonic-gate	    $numopen -= @lru;
33*0Sstevel@tonic-gate	    for (@lru) { &close($_); delete $isopen{$_}; }
34*0Sstevel@tonic-gate	}
35*0Sstevel@tonic-gate	&open($file, ($saw{$file}++ ? '>>' : '>') . $file)
36*0Sstevel@tonic-gate	    || die "Can't create $file: $!\n";
37*0Sstevel@tonic-gate    }
38*0Sstevel@tonic-gate    $isopen{$file} = ++$seq;
39*0Sstevel@tonic-gate}
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gatepackage cacheout;
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate$seq = 0;
44*0Sstevel@tonic-gate$numopen = 0;
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gateif (open(PARAM,'/usr/include/sys/param.h')) {
47*0Sstevel@tonic-gate    local($_, $.);
48*0Sstevel@tonic-gate    while (<PARAM>) {
49*0Sstevel@tonic-gate	$maxopen = $1 - 4 if /^\s*#\s*define\s+NOFILE\s+(\d+)/;
50*0Sstevel@tonic-gate    }
51*0Sstevel@tonic-gate    close PARAM;
52*0Sstevel@tonic-gate}
53*0Sstevel@tonic-gate$maxopen = 16 unless $maxopen;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate1;
56