File Coverage

blib/lib/File/Index.pm
Criterion Covered Total %
statement 15 55 27.2
branch 0 16 0.0
condition 0 11 0.0
subroutine 5 7 71.4
pod 2 2 100.0
total 22 91 24.1


line stmt bran cond sub pod time code
1             package File::Index;
2 1     1   47464 use strict;
  1         3  
  1         59  
3 1     1   7 use warnings;
  1         2  
  1         38  
4              
5 1     1   5 use Exporter ();
  1         7  
  1         33  
6 1     1   6 use Carp qw(croak);
  1         2  
  1         90  
7 1     1   7 use vars qw($VERSION @ISA @EXPORT);
  1         2  
  1         630  
8              
9             $VERSION = "0.06";
10             @ISA = qw(Exporter);
11             @EXPORT = qw(indexf rindexf);
12              
13             sub indexf {
14 0     0 1   my $filehandle=shift;
15 0           my $substring=shift;
16 0   0       my $start=shift||0;
17 0   0       my $bufferSize=shift||131072;
18 0           my $k=length($substring);
19 0           my $offset=0;
20 0           my $s="";
21 0 0         croak "BufferSize must not be less than substring length"
22             if $bufferSize<$k;
23             # Seek to start point; use successive reads if file isn't seekable
24 0 0         if ( ! seek($filehandle,$start,0) ) {
25 0           for (my $j=0;$j
26 0 0         read($filehandle,$s,$bufferSize) or return(-1)
27             }
28 0 0         if ( $start%($bufferSize) > 0 ) {
29 0 0         read($filehandle,$s,$start%($bufferSize)) or return(-1)
30             }
31 0           $s=""
32             }
33             # Read and append to end of preserved string
34 0           while ( read($filehandle,substr($s,length($s)),$bufferSize) > 0 ) {
35 0 0         if ( (my $n=index($s,$substring)) > -1 ) { return($n+$offset+$start) }
  0            
36 0           $offset+=(length($s)-$k+1);
37             # Preserve last ($k-1) characters
38 0           $s=substr($s,-$k+1)
39             }
40 0           return(-1)
41             }
42              
43             sub rindexf {
44 0     0 1   my $filehandle=shift;
45 0           my $substring=shift;
46 0   0       my $beg=shift||-1;
47 0   0       my $bufferSize=shift||131072;
48 0           my $k=length($substring);
49 0           my $offset=0;
50 0           my $s="";
51 0           my $match=-1;
52 0 0         croak "BufferSize must not be less than substring length" if $bufferSize<$k;
53 0           seek($filehandle,0,0);
54 0           while (read($filehandle,substr($s,length($s)),$bufferSize)) {
55             # Read and append to end of preserved string
56 0           my $j=0;
57 0           while ( (my $n=index($s,$substring,$j)) > -1 ) {
58 0 0 0       if ( ($beg>=0) && (($n+$offset)>$beg) ) { return($match) }
  0            
59 0           else { $match=$n+$offset; $j=$n+1 }
  0            
60             }
61 0           $offset+=(length($s)-$k+1);
62             # Preserve last ($k-1) characters
63 0           $s=substr($s,-$k+1)
64             }
65             # Return last match-position
66 0           return($match)
67             }
68              
69             =head1 NAME
70              
71             File::Index - an index function for files
72              
73             =head1 SYNOPSIS
74              
75             use File::Index;
76             open(FILE,$myfile);
77             my $pos=indexf(*FILE,"Foo");
78             print "Foo found at position: $pos\n" if $pos > -1;
79             open(FILE2,$myfile2);
80             my $pos2=rindexf(*FILE2,"Bar");
81             print "Bar found at position: $pos2\n" if $pos2 > -1;
82              
83             =head1 DESCRIPTION
84              
85             This module provides the indexf and rindexf functions which operate on files in the
86             same way that the index and rindex functions operate on strings. It can be used where
87             memory limitations prohibit the slurping of an entire file.
88              
89             =over 4
90              
91             =item C
92              
93             Starts at the position specified by '$start' (or at the beginning) of
94             the file associated with filehandle 'FH', and returns the absolute start
95             position of the string '$string'.
96             The buffer-size can be adjusted by specifying '$buffersize'.
97              
98             =item C
99              
100             Returns the position of the last occurrence of '$string' in the file
101             associated with filehandle 'FH'. If '$position' is specified, returns
102             the last occurrence beginning at or before '$position'.
103              
104             The buffer-size can be adjusted by specifying '$buffersize'. If you
105             wish to specify a '$buffersize' value without specifying a '$position'
106             value, use a negative value (e.g. '-1') for the latter.
107              
108              
109             =back
110              
111             =head1 AUTHOR
112              
113             Graham Jenkins, C<< >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to C, or through
118             the web interface at L. I will be notified, and then you'll
119             automatically be notified of progress on your bug as I make changes.
120              
121              
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc File::Index
128              
129              
130             You can also look for information at:
131              
132             =over 4
133              
134             =item * RT: CPAN's request tracker (report bugs here)
135              
136             L
137              
138             =item * AnnoCPAN: Annotated CPAN documentation
139              
140             L
141              
142             =item * CPAN Ratings
143              
144             L
145              
146             =item * Search CPAN
147              
148             L
149              
150             =back
151              
152              
153             =head1 ACKNOWLEDGEMENTS
154              
155              
156             =head1 LICENSE AND COPYRIGHT
157              
158             Copyright 2012 Graham Jenkins.
159              
160             This program is free software; you can redistribute it and/or modify it
161             under the terms of either: the GNU General Public License as published
162             by the Free Software Foundation; or the Artistic License.
163              
164             See http://dev.perl.org/licenses/ for more information.
165              
166              
167             =cut
168              
169             1; # End of File::Index