File Coverage

blib/lib/IO/Mark/Cache.pm
Criterion Covered Total %
statement 38 42 90.4
branch 9 10 90.0
condition 5 6 83.3
subroutine 9 9 100.0
pod n/a
total 61 67 91.0


line stmt bran cond sub pod time code
1             package IO::Mark::Cache;
2              
3 2     2   11 use strict;
  2         4  
  2         58  
4 2     2   9 use warnings;
  2         11  
  2         45  
5 2     2   9 use Carp;
  2         9  
  2         852  
6              
7             sub _new {
8 5     5   10 my $class = shift;
9 5         8 my $fh = shift;
10              
11 5         42 return bless {
12             fh => $fh,
13             buf => '',
14             eof => 0,
15             ref_count => 1,
16              
17             # Field we maintain on behalf of the IO::Mark::Buffer that gets
18             # added to the master file handle
19             master_pos => 0,
20             }, $class;
21             }
22              
23             sub _get_master_pos {
24 1199     1199   1131 my $self = shift;
25 1199         2557 return $self->{master_pos};
26             }
27              
28             sub _inc_master_pos {
29 1193     1193   1526 my $self = shift;
30 1193         1055 my $inc = shift;
31 1193         2237 $self->{master_pos} += $inc;
32             }
33              
34             sub _inc_ref_count {
35 6     6   6 my $self = shift;
36 6         13 return ++$self->{ref_count};
37             }
38              
39             sub _dec_ref_count {
40 11     11   13 my $self = shift;
41              
42 11         47 my $count = --$self->{ref_count};
43              
44 11 100       28 if ( $count == 0 ) {
45 5         20 $self->{fh}->close;
46             }
47              
48 11         115 return $count;
49             }
50              
51             sub _read {
52 2394     2394   2259 my $self = shift;
53              
54             # my ($buf, $len, $pos) = @_;
55              
56 2394         2201 my $got = 0;
57 2394         2758 my $fh = $self->{fh};
58              
59             # Only buffer if there is more than one handle watching
60 2394 100 100     8370 if ( $self->{ref_count} > 1 && !$self->{eof} ) {
61 1252         1840 my $want = ( $_[2] + $_[1] ) - length( $self->{buf} );
62 1252 100       2061 if ( $want > 0 ) {
63 1193         3794 my $got = $fh->read( $self->{buf}, $want, length( $self->{buf} ) );
64 1193         7634 $self->{eof} = $want > $got;
65             }
66             }
67              
68             # How much in buffer?
69 2394         3233 my $avail = length( $self->{buf} ) - $_[2];
70 2394 100       4028 $avail = $_[1] if $avail > $_[1];
71              
72             # Read the data into the supplied buffer
73 2394         3788 $_[0] = substr $self->{buf}, $_[2], $avail;
74 2394         2203 $got = $avail;
75              
76             # If the buffer is exhausted but we're not at eof read some more.
77             # Once we're in single watcher mode and the buffer is empty all
78             # reads come straight here.
79 2394 50 66     6902 if ( !$self->{eof} && $got < $_[1] ) {
80 0         0 my $want = $_[1] - $got;
81 0         0 my $got2 = $fh->read( $_[0], $want, length( $_[0] ) );
82 0         0 $self->{eof} = $want > $got2;
83 0         0 $got += $got2;
84             }
85              
86 2394         5104 return $got;
87             }
88              
89             1;
90              
91             =head1 NAME
92              
93             IO::Mark::Cache - Stream cache for IO::Mark
94              
95             =head1 VERSION
96              
97             This document describes IO::Mark version 0.0.1
98              
99             =head1 SYNOPSIS
100              
101             Don't use IO::Mark::Cache directly; it has no usable public interface.
102             Use instead L.
103              
104             =head1 BUGS AND LIMITATIONS
105              
106             No bugs have been reported.
107              
108             Please report any bugs or feature requests to
109             C, or through the web interface at
110             L.
111              
112             =head1 AUTHOR
113              
114             Andy Armstrong C<< >>
115              
116             =head1 LICENCE AND COPYRIGHT
117              
118             Copyright (c) 2007, Andy Armstrong C<< >>. All rights reserved.
119              
120             This module is free software; you can redistribute it and/or
121             modify it under the same terms as Perl itself. See L.
122              
123             =head1 DISCLAIMER OF WARRANTY
124              
125             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
126             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
127             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
128             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
129             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
130             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
131             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
132             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
133             NECESSARY SERVICING, REPAIR, OR CORRECTION.
134              
135             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
136             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
137             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
138             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
139             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
140             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
141             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
142             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
143             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
144             SUCH DAMAGES.