summaryrefslogtreecommitdiff
path: root/lib/cacheout.pl
blob: 789c9102c51d47c9f55005f24c7f4485322e7133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
# This library is no longer being maintained, and is included for backward
# compatibility with Perl 4 programs which may require it.
# This legacy library is deprecated and will be removed in a future
# release of perl.
#
# In particular, this should not be used as an example of modern Perl
# programming techniques.
#
# Suggested alternative: FileCache

warn( "The 'cacheout.pl' legacy library is deprecated and will be"
      . " removed in the next major release of perl. Please use the"
      . " FileCache module instead." );

# Open in their package.

sub cacheout'open {
    open($_[0], $_[1]);
}

# Close as well

sub cacheout'close {
    close($_[0]);
}

# But only this sub name is visible to them.

sub cacheout {
    package cacheout;

    ($file) = @_;
    if (!$isopen{$file}) {
	if (++$numopen > $maxopen) {
	    local(@lru) = sort {$isopen{$a} <=> $isopen{$b};} keys(%isopen);
	    splice(@lru, $maxopen / 3);
	    $numopen -= @lru;
	    for (@lru) { &close($_); delete $isopen{$_}; }
	}
	&open($file, ($saw{$file}++ ? '>>' : '>') . $file)
	    || die "Can't create $file: $!\n";
    }
    $isopen{$file} = ++$seq;
}

package cacheout;

$seq = 0;
$numopen = 0;

if (open(PARAM,'/usr/include/sys/param.h')) {
    local($_, $.);
    while (<PARAM>) {
	$maxopen = $1 - 4 if /^\s*#\s*define\s+NOFILE\s+(\d+)/;
    }
    close PARAM;
}
$maxopen = 16 unless $maxopen;

1;