| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# -*- perl -*- |
|
2
|
|
|
|
|
|
|
# |
|
3
|
|
|
|
|
|
|
# IO::File::Cached by Daniel Berrange |
|
4
|
|
|
|
|
|
|
# |
|
5
|
|
|
|
|
|
|
# Copyright (C) 20004 Daniel P. Berrange |
|
6
|
|
|
|
|
|
|
# |
|
7
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by |
|
9
|
|
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
|
|
|
|
# (at your option) any later version. |
|
11
|
|
|
|
|
|
|
# |
|
12
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
|
13
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
|
|
|
|
# GNU General Public License for more details. |
|
16
|
|
|
|
|
|
|
# |
|
17
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
|
18
|
|
|
|
|
|
|
# along with this program; if not, write to the Free Software |
|
19
|
|
|
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20
|
|
|
|
|
|
|
# |
|
21
|
|
|
|
|
|
|
# $Id: Cached.pm,v 1.4 2004/04/01 19:11:23 dan Exp $ |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=pod |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
IO::File::Cached - an caching file IO handle |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use IO::File::Cached; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Open a file and read it line by line |
|
34
|
|
|
|
|
|
|
my $fh = IO::File::Cached->new(filename => $filename, |
|
35
|
|
|
|
|
|
|
cache => $cache); |
|
36
|
|
|
|
|
|
|
while (defined ($_ = $fh->read)) { |
|
37
|
|
|
|
|
|
|
print "Got line: $_\n"; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
$fh->close; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The IO::File::Cached module is a subclass of the IO::Scalar module |
|
44
|
|
|
|
|
|
|
that uses IO::File and Cache::Cache modules to access the contents |
|
45
|
|
|
|
|
|
|
of a file. The first time it is called for a particular file name it |
|
46
|
|
|
|
|
|
|
will load the file as normal using IO::File. The entire contents of |
|
47
|
|
|
|
|
|
|
the file will then be stored in the supplied cache object, such that |
|
48
|
|
|
|
|
|
|
later loads do not have to read from disk. One situation in which |
|
49
|
|
|
|
|
|
|
this can be useful is to cache files in memory across all processes |
|
50
|
|
|
|
|
|
|
in a mod_perl server. |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 METHODS |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=over 4 |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
package IO::File::Cached; |
|
59
|
|
|
|
|
|
|
|
|
60
|
1
|
|
|
1
|
|
1376
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
47
|
|
|
61
|
1
|
|
|
1
|
|
1107
|
use IO::Scalar; |
|
|
1
|
|
|
|
|
24525
|
|
|
|
1
|
|
|
|
|
48
|
|
|
62
|
1
|
|
|
1
|
|
989
|
use IO::File; |
|
|
1
|
|
|
|
|
2522
|
|
|
|
1
|
|
|
|
|
158
|
|
|
63
|
1
|
|
|
1
|
|
9
|
use Carp qw(confess); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
43
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION $RELEASE @ISA); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
706
|
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
@ISA = qw(IO::Scalar); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$VERSION = "1.0.1"; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item new(filename => $filename[, cache => $cache] ); |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Creates a new IO::File::Cached object. If the cache parameter is |
|
74
|
|
|
|
|
|
|
supplied, this cache object will be used to load and store the |
|
75
|
|
|
|
|
|
|
file contents. At this time, instances of IO::File::Cached are |
|
76
|
|
|
|
|
|
|
read only. The object specified by the 'cache' parameter should |
|
77
|
|
|
|
|
|
|
be an instance of the Cache::Cache module. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new { |
|
82
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
|
83
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $proto; |
|
84
|
0
|
|
|
|
|
|
my %params = @_; |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
my $filename = exists $params{filename} ? $params{filename} : confess "filename parameter is required"; |
|
87
|
0
|
0
|
|
|
|
|
my $cache = exists $params{cache} ? $params{cache} : undef; |
|
88
|
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
my $data; |
|
90
|
0
|
0
|
|
|
|
|
if ($filename eq '-') { |
|
91
|
0
|
|
|
|
|
|
local $/ = undef; |
|
92
|
0
|
|
|
|
|
|
$data = ; |
|
93
|
|
|
|
|
|
|
} else { |
|
94
|
0
|
0
|
|
|
|
|
if (defined $cache) { |
|
95
|
0
|
|
|
|
|
|
$data = $cache->get($filename); |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if (!defined $data) { |
|
99
|
0
|
0
|
|
|
|
|
my $fh = IO::File->new($filename) or |
|
100
|
|
|
|
|
|
|
confess "cannot load file $filename: $!";; |
|
101
|
0
|
|
|
|
|
|
local $/ = undef; |
|
102
|
0
|
|
|
|
|
|
$data = <$fh>; |
|
103
|
0
|
0
|
|
|
|
|
$fh->close or |
|
104
|
|
|
|
|
|
|
confess "cannot close file $filename: $!"; |
|
105
|
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
if (defined $cache) { |
|
107
|
0
|
|
|
|
|
|
$cache->set($filename, $data); |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(\$data); |
|
113
|
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
bless $self, $class; |
|
115
|
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return $self; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1 # So that the require or use succeeds. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |