| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | package File::Bidirectional; | ||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Fcntl qw/:seek O_RDONLY/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =pod | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | File::Bidirectional - Read a file line-by-line either forwards or backwards | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | use File::Bidirectional; | ||||||||||||||||||||||||||||||||
| 21 | my $file = "/var/log/large_file"; | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # Object interface | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # start from the last line | ||||||||||||||||||||||||||||||||
| 26 | my $fh = File::Bidirectional->new($file, {origin => -1}) | ||||||||||||||||||||||||||||||||
| 27 | or die $!; | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # read backwards until point of interest | ||||||||||||||||||||||||||||||||
| 30 | while (my $line = $fh->readline()) { | ||||||||||||||||||||||||||||||||
| 31 | last if $line =~ /RECORD_START/; | ||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # switch directions | ||||||||||||||||||||||||||||||||
| 35 | $fh->switch(); | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | # read forwards until point of interest | ||||||||||||||||||||||||||||||||
| 38 | while (my $line = $fh->readline()) { | ||||||||||||||||||||||||||||||||
| 39 | last if $line =~ /RECORD_END/; | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # Tied Handle Interface | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | local *F; | ||||||||||||||||||||||||||||||||
| 45 | tie *F, "File::Bidirectional", $file, {origin => 1} | ||||||||||||||||||||||||||||||||
| 46 | or die $!; | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | while (my $line = <F>) { ... } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | (tied *F)->switch(); | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | File::Bidirectional reads a file line-by-line in either the forwards or | ||||||||||||||||||||||||||||||||
| 55 | backwards direction. It supports an object interface as well as a tied | ||||||||||||||||||||||||||||||||
| 56 | filehandle interface, and should be straight-forward to use. It is also memory | ||||||||||||||||||||||||||||||||
| 57 | efficient, since it is intended to be used on files too large to be efficiently | ||||||||||||||||||||||||||||||||
| 58 | slurped into an array and traversed backwards. | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | The direction in which to traverse the file can be changed at anytime, but it is | ||||||||||||||||||||||||||||||||
| 61 | important to note that the last-read line will be repeated when this happens. | ||||||||||||||||||||||||||||||||
| 62 | See C<line_num> to see why this is so. | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | On non-Unix platforms, this module attempts to immitate native Perl in | ||||||||||||||||||||||||||||||||
| 65 | converting the line endings. Currently, this is limited and untested, so please | ||||||||||||||||||||||||||||||||
| 66 | see L</LINE ENDINGS> for more information. | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | =head1 MOTIVATION | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | I had a C<diff> file describing the changes in a large (> 200MB) file. Based on | ||||||||||||||||||||||||||||||||
| 71 | the line numbers in the C<diff>, I have to repeatedly read backwards and | ||||||||||||||||||||||||||||||||
| 72 | forwards in the large file to obtain the context lines before and after the | ||||||||||||||||||||||||||||||||
| 73 | C<diff> changes. The number of context lines vary, thus it was a little more | ||||||||||||||||||||||||||||||||
| 74 | involved than regenerating the C<diff> with an appropriate C<--context> option. | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | I decided to publish this module as I thought others might have similar needs. | ||||||||||||||||||||||||||||||||
| 77 | Reading large log files backwards is probably the most common of these, but if | ||||||||||||||||||||||||||||||||
| 78 | you have any other interesting uses, do let me know. | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # globals | ||||||||||||||||||||||||||||||||
| 83 | 2 | my ($BLOCK_SIZE); | |||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 85 | # defaults - can be changed through constructor | ||||||||||||||||||||||||||||||||
| 86 | 2 | $BLOCK_SIZE = 1024 * 8; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # _read_line() and _eof() are used as sensible defaults. we will fix up the | ||||||||||||||||||||||||||||||||
| 89 | # aliases again later to optimize away one indirection function call | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | # tied interface | ||||||||||||||||||||||||||||||||
| 92 | 2 | *TIEHANDLE = \&new; | |||||||||||||||||||||||||||||||
| 93 | 2 | *READLINE = \&_read_line; | |||||||||||||||||||||||||||||||
| 94 | 2 | *EOF = \&_eof; | |||||||||||||||||||||||||||||||
| 95 | 2 | *CLOSE = \&close; | |||||||||||||||||||||||||||||||
| 96 | 2 | *TELL = \&tell; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | # IO::Handle compatability | ||||||||||||||||||||||||||||||||
| 99 | 2 | *getline = \&_read_line; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | # File::ReadBackwards compatability | ||||||||||||||||||||||||||||||||
| 102 | 2 | *get_handle = \&fh; | |||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | # aliases | ||||||||||||||||||||||||||||||||
| 105 | 2 | *readline = \&_read_line; | |||||||||||||||||||||||||||||||
| 106 | 2 | *eof = \&_eof; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | =pod | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | =head1 CONSTRUCTOR (CLASS METHODS) | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =over | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =item new $file, \%option | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | $fh = File::Bidirectional->new($file); | ||||||||||||||||||||||||||||||||
| 118 | $fh = File::Bidirectional->new($file, {mode => 'forward'}); | ||||||||||||||||||||||||||||||||
| 119 | $fh = File::Bidirectional->new($file, {mode => 'backward'}); | ||||||||||||||||||||||||||||||||
| 120 | $fh = File::Bidirectional->new($file, {origin => -1}); | ||||||||||||||||||||||||||||||||
| 121 | $fh = File::Bidirectional->new($file, \%option); | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | Has the file name as the first parameter, and a hashref of options as an | ||||||||||||||||||||||||||||||||
| 124 | optional second parameter. Upon success, it will return the object. For invalid | ||||||||||||||||||||||||||||||||
| 125 | parameters, it will C<Carp/croak>. For L<perlfunc/sysopen> errors, it returns | ||||||||||||||||||||||||||||||||
| 126 | undef and sets the error code in L<perlvar/$!>. | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | The list of valid options are: | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | =over | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | =item mode | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | Can be either C<bi> (bi-directional), C<forward> or C<backward>. The C<forward> | ||||||||||||||||||||||||||||||||
| 135 | and C<backward> modes are restrictive: the file is read from the first and last | ||||||||||||||||||||||||||||||||
| 136 | line respectively, and switching directions is prohibited. The C<bi> mode | ||||||||||||||||||||||||||||||||
| 137 | allows direction switching, and will start from the first line by default (use | ||||||||||||||||||||||||||||||||
| 138 | the C<origin> option to change that.) The default is C<bi>. | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | =item origin | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | Can be either C<1> or C<-1>. These denote whether the first or last line of the | ||||||||||||||||||||||||||||||||
| 143 | file is considered as line 1 by C<line_num>. (C<readline> will always start | ||||||||||||||||||||||||||||||||
| 144 | from line 1.) C<origin> can only be set if the C<mode> option is C<bi>. The | ||||||||||||||||||||||||||||||||
| 145 | default is C<1>. | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | =item binmode | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | Can be any true or false expression. It is analogous to the L<perlfunc/binmode> | ||||||||||||||||||||||||||||||||
| 150 | built-in function. On systems that distinguish between binary and text files, | ||||||||||||||||||||||||||||||||
| 151 | notably DOS and Windows-based systems, this is important. A true value will | ||||||||||||||||||||||||||||||||
| 152 | preserve C<\r\n> as is; a false value will convert C<\r\n> to C<\n>. The | ||||||||||||||||||||||||||||||||
| 153 | default is false. | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =item separator | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | Can be any scalar string. It is analogous to the L<perlvar/"$/"> variable. | ||||||||||||||||||||||||||||||||
| 158 | C<separator> determines C<File::Bidirectional>'s notion of what a line is. The | ||||||||||||||||||||||||||||||||
| 159 | default is L<perlvar/"$/">, which in turn defaults to C<"\n">. | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | Caveat: The Perl-ish magic that occurs when L<perlvar/"$/"> is C<""> does not | ||||||||||||||||||||||||||||||||
| 162 | happen yet. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =item regex | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | Can be any true or false expression. It determines whether the C<separator> | ||||||||||||||||||||||||||||||||
| 167 | option is a regex or a string. The default is false. | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | =item block_size | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | Can be any positive integer. This is the size of a single block read by the | ||||||||||||||||||||||||||||||||
| 172 | underlying L<perlfunc/sysread>. The default is 8192. | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | =back | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | =head1 INSTANCE METHODS | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=27 SCAR=66.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 184 | 0 | my ($class, $file, $option) = @_; | |||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | croak "expected class method" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | unless defined $class; | ||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | croak "expected filename" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | unless defined $file; | ||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | croak "expected hashref for parameters" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $option or ref $option eq "HASH"
MC/DC0%0/2
not defined $option or ref $option eq "HASH"
not defined $option
ref $option eq "HASH"
Truth table0%0/3
not(A) or B
A= not defined $optionB= ref $option eq "HASH"
| |||||||||||||||||||||||||||||||||
| 190 | unless !defined $option || ref($option) eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | # block size and buffer size | ||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $block_size = $option->{block_size} || $BLOCK_SIZE; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$option->{'block_size'} || $BLOCK_SIZE
MC/DC0%0/2
$option->{'block_size'} || $BLOCK_SIZE
$option->{'block_size'}
$BLOCK_SIZE
Truth table0%0/3
A || B
A= $option->{'block_size'}B= $BLOCK_SIZE
| |||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | croak "expected block_size to be positive integer" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$block_size =~ /^\d+$/ and $block_size > 0
MC/DC0%0/2
$block_size =~ /^\d+$/ and $block_size > 0
$block_size =~ /^\d+$/
$block_size > 0
Truth table0%0/3
A and B
A= $block_size =~ /^\d+$/B= $block_size > 0
| |||||||||||||||||||||||||||||||||
| 195 | unless $block_size =~ /^\d+$/ && $block_size > 0; | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | # default separator is $/ | ||||||||||||||||||||||||||||||||
| 198 | 0 | my $sep = $option->{separator}; | |||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $sep = $/ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | unless defined $sep; | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | # default is not to treat separator as regex | ||||||||||||||||||||||||||||||||
| 203 | 0 | my $sep_re = $option->{regex}; | |||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | $sep_re = 0 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 205 | unless defined $sep_re; | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | # pre-compile regular expression | ||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $re = ($sep_re) ? qr/(.*?$sep|.+)/ : qr/(.*?\Q$sep\E|.+)/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | # translation takes place on DOS (without binmode), Mac etc. | ||||||||||||||||||||||||||||||||
| 211 | 0 | my $binmode = $option->{binmode}; | |||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | my $translate = | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
_is_dos() and not $binmode
MC/DC0%0/2
_is_dos() and not $binmode
_is_dos()
not $binmode
Truth table0%0/3
A and not(B)
A= _is_dos()B= not $binmode
| |||||||||||||||||||||||||||||||||
| 213 | (_is_dos() && !$binmode) ? qr/\015\012/ : | ||||||||||||||||||||||||||||||||
| 214 | (_is_mac()) ? qr/\015/ : | ||||||||||||||||||||||||||||||||
| 215 | undef; | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | # default mode is bidirectional | ||||||||||||||||||||||||||||||||
| 218 | 0 | my $mode = $option->{mode}; | |||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | croak "expected mode to be [bi|forward|backward]" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $mode or $mode =~ /^(bi|forward|backward)$/
MC/DC0%0/2
not defined $mode or $mode =~ /^(bi|forward|backward)$/
not defined $mode
$mode =~ /^(bi|forward|backward)$/
Truth table0%0/3
not(A) or B
A= not defined $modeB= $mode =~ /^(bi|forward|backward)$/
| |||||||||||||||||||||||||||||||||
| 220 | unless !defined $mode || $mode =~ /^(bi|forward|backward)$/; | ||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | $mode = 'bi' unless defined $mode; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | # origin can only be explicitly set for bidirectional | ||||||||||||||||||||||||||||||||
| 224 | 0 | my $origin = $option->{origin}; | |||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | croak "expected origin only for mode \"bi\"" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $origin or $mode eq "bi"
MC/DC0%0/2
not defined $origin or $mode eq "bi"
not defined $origin
$mode eq "bi"
Truth table0%0/3
not(A) or B
A= not defined $originB= $mode eq "bi"
| |||||||||||||||||||||||||||||||||
| 226 | unless !defined $origin || $mode eq 'bi'; | ||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | croak "expected origin to be [1|-1]" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $origin or $origin =~ /^(1|-1)$/
MC/DC0%0/2
not defined $origin or $origin =~ /^(1|-1)$/
not defined $origin
$origin =~ /^(1|-1)$/
Truth table0%0/3
not(A) or B
A= not defined $originB= $origin =~ /^(1|-1)$/
| |||||||||||||||||||||||||||||||||
| 228 | unless !defined $origin || $origin =~ /^(1|-1)$/; | ||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if (!defined $origin) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | $origin = | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 231 | ($mode eq 'bi') ? 1 : | ||||||||||||||||||||||||||||||||
| 232 | ($mode eq 'forward') ? 1 : | ||||||||||||||||||||||||||||||||
| 233 | ($mode eq 'backward') ? -1 : undef; | ||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | # file size | ||||||||||||||||||||||||||||||||
| 237 | 0 | my $file_size = -s $file; | |||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | # set starting point of cursor to coincide with the origin | ||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | my $start = ($origin == 1) ? 0 : $file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | sysopen my $fh, $file, O_RDONLY | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | or return undef; | ||||||||||||||||||||||||||||||||
| 244 | 0 | binmode $fh; | |||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 0 | my $x = { | |||||||||||||||||||||||||||||||
| 247 | mode => $mode, # mode | ||||||||||||||||||||||||||||||||
| 248 | fh => $fh, # filehandle | ||||||||||||||||||||||||||||||||
| 249 | cur => $start, # physical cursor on filehandle | ||||||||||||||||||||||||||||||||
| 250 | buffer => [], # buffer | ||||||||||||||||||||||||||||||||
| 251 | origin => $origin, # 1: first line as line 1 / -1: last line as line 1 | ||||||||||||||||||||||||||||||||
| 252 | move => $origin, # 1: moving forwards / -1: moving backwards | ||||||||||||||||||||||||||||||||
| 253 | line => 0, # forward: line read / backward: line to be read | ||||||||||||||||||||||||||||||||
| 254 | re => $re, # regular expression for separator | ||||||||||||||||||||||||||||||||
| 255 | translate => $translate, | ||||||||||||||||||||||||||||||||
| 256 | file_size => $file_size, | ||||||||||||||||||||||||||||||||
| 257 | block_size => $block_size, | ||||||||||||||||||||||||||||||||
| 258 | }; | ||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 0 | bless ($x, $class); | |||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | # fixup the aliases to save a method call for readline | ||||||||||||||||||||||||||||||||
| 263 | 0 | $x->_fixup_alias(); | |||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 0 | return $x; | |||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | =pod | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | =over | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | =item readline | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | while (my $line = $fh->readline()) { ... } | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | Returns the subsequent line. This refers either to the next line when the | ||||||||||||||||||||||||||||||||
| 277 | direction is forwards, or to the previous line when the direction is backwards. | ||||||||||||||||||||||||||||||||
| 278 | The direction can be changed with C<switch>. C<undef> is returned when there | ||||||||||||||||||||||||||||||||
| 279 | are no more lines to be read. | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | =item getline | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | An alias for C<readline>. It exists for compatability with the IO::* classes. | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | =item eof | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | Returns true when C<readline> will return an C<undef>, false otherwise. | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =item switch | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | $fh->switch(); | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | Switches the current direction in which we are reading the file. It will | ||||||||||||||||||||||||||||||||
| 294 | L<Carp/croak> if the C<mode> option in the constructor is set to C<forward> or | ||||||||||||||||||||||||||||||||
| 295 | C<backward>. | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | Note that switching directions will cause the last-read line to be repeated by | ||||||||||||||||||||||||||||||||
| 298 | C<readline>. | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | =cut | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | # reverse movement direction | ||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | sub switch { | ||||||||||||||||||||||||||||||
|
Subroutine
switch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 304 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | croak "needs to be bidirectional mode to switch directions" | ||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | unless $x->{mode} eq 'bi'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | # get current tell() before changing direction and invalidating the buffer | ||||||||||||||||||||||||||||||||
| 310 | 0 | $x->{cur} = $x->tell(); | |||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||
| 312 | # invalidate the buffer | ||||||||||||||||||||||||||||||||
| 313 | 0 | undef @{$x->{buffer}}; | |||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | # change direction | ||||||||||||||||||||||||||||||||
| 316 | 0 | $x->{move} *= -1; | |||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | # fixup aliases for readline() and eof() after changing direction | ||||||||||||||||||||||||||||||||
| 319 | 0 | $x->_fixup_alias(); | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | =pod | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | =item close | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | $fh->close(); | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | Closes the underlying filehandle and releases the memory allocated for its | ||||||||||||||||||||||||||||||||
| 329 | buffer. On success it returns true, otherwise it returns false with the error | ||||||||||||||||||||||||||||||||
| 330 | code found in L<perlvar/$!>. All subsequent C<readline> calls will return | ||||||||||||||||||||||||||||||||
| 331 | undef, and C<line_num>, its last value. | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | # close file and destroy state | ||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 337 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 338 | 0 | undef @{$x->{buffer}}; | |||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | $x->{cur} = ($x->{move} == 1) ? $x->{file_size} : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 340 | CORE::close($x->{fh}) | ||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 342 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | =pod | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | =item direction $direction | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | Takes an optional parameter: 1 for reading forwards, -1 for reading backwards, | ||||||||||||||||||||||||||||||||
| 350 | L<Carp/croak> otherwise. If an argument for the parameter is provided, the | ||||||||||||||||||||||||||||||||
| 351 | direction will be switched if necessary. Either way, it returns the (new) | ||||||||||||||||||||||||||||||||
| 352 | direction. | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | =cut | ||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub direction { | ||||||||||||||||||||||||||||||
|
Subroutine
direction: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 357 | 0 | my ($x, $direction) = @_; | |||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | croak "expected direction to be [1|-1]" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $direction or $direction =~ /^(1|-1)$/
MC/DC0%0/2
not defined $direction or $direction =~ /^(1|-1)$/
not defined $direction
$direction =~ /^(1|-1)$/
Truth table0%0/3
not(A) or B
A= not defined $directionB= $direction =~ /^(1|-1)$/
| |||||||||||||||||||||||||||||||||
| 359 | unless !defined $direction || $direction =~ /^(1|-1)$/; | ||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if (defined $direction && $direction != $x->{move}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $direction and $direction != $x->{"move"}
MC/DC0%0/2
defined $direction and $direction != $x->{"move"}
defined $direction
$direction != $x->{"move"}
Truth table0%0/3
A and B
A= defined $directionB= $direction != $x->{"move"}
| |||||||||||||||||||||||||||||||||
| 362 | 0 | $x->switch(); | |||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 0 | return $x->{move}; | |||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | =pod | ||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | =item line_num | ||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | my $fh=File::Bidirectional->new($file); n=$fh->line_num(); # n = 0 | ||||||||||||||||||||||||||||||||
| 373 | $fh->readline(); n=$fh->line_num(); # n = 1 | ||||||||||||||||||||||||||||||||
| 374 | $fh->readline(); n=$fh->line_num(); # n = 2 | ||||||||||||||||||||||||||||||||
| 375 | $fh->switch(); n=$fh->line_num(); # n = 2 | ||||||||||||||||||||||||||||||||
| 376 | $fh->readline(); n=$fh->line_num(); # n = 1 | ||||||||||||||||||||||||||||||||
| 377 | $fh->readline(); n=$fh->line_num(); # n = 0 | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | Returns the current line number. It is analogous to L<perlvar/$.>. | ||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | For a file with I<n> logical lines, the line number ranges from 0 to I<n>. When | ||||||||||||||||||||||||||||||||
| 382 | reading away from the origin (forwards if the first line is the origin), its | ||||||||||||||||||||||||||||||||
| 383 | behavior is always identical to that of L<perlvar/$.> - it refers to the number | ||||||||||||||||||||||||||||||||
| 384 | of lines that has been read. When reading towards the origin, it refers to the | ||||||||||||||||||||||||||||||||
| 385 | number of lines that can still be read. | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | When C<switch> is called, the direction is changed, but the line number | ||||||||||||||||||||||||||||||||
| 388 | remains the same. Therefore, the last-read line before changing directions will | ||||||||||||||||||||||||||||||||
| 389 | be repeated by C<readline>. | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | # current line number, 1-based | ||||||||||||||||||||||||||||||||
| 394 | # forward: the line that has just been read | ||||||||||||||||||||||||||||||||
| 395 | # backward: the line that is going to be read | ||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | sub line_num { | ||||||||||||||||||||||||||||||
|
Subroutine
line_num: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 397 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 398 | 0 | return $x->{line}; | |||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | =pod | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | =item tell | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | Returns the current position of the filehandle. | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | =cut | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | # logical cursor on filehandle | ||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | sub tell { | ||||||||||||||||||||||||||||||
|
Subroutine
tell: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 411 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 412 | 0 | my $pos = 0; | |||||||||||||||||||||||||||||||
| 413 | 0 | for my $s (@{$x->{buffer}}) { | |||||||||||||||||||||||||||||||
| 414 | 0 | $pos += length $s; | |||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | return ($x->{move} == 1) ? $x->{cur} - $pos : $x->{cur} + $pos; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | =pod | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | =item fh | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | Returns the underlying filehandle. This is mainly useful for file-locking. | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | Notice that this actually breaks the encapsulation of File::Bidirectional, | ||||||||||||||||||||||||||||||||
| 426 | therefore it becomes the user's responsibility to ensure that nothing bad | ||||||||||||||||||||||||||||||||
| 427 | happens to the underlying filehandle. For example, it should definitely not be | ||||||||||||||||||||||||||||||||
| 428 | closed. | ||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | The underlying filehandle will be returned with its seek position set to what is | ||||||||||||||||||||||||||||||||
| 431 | returned by C<tell>. It should generally be okay for this seek position to be | ||||||||||||||||||||||||||||||||
| 432 | modified (the object remembers its own seek position and will always restore | ||||||||||||||||||||||||||||||||
| 433 | it). Any other operations on the filehandle, however, is very likely to void | ||||||||||||||||||||||||||||||||
| 434 | your warranty. =) | ||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | sub fh { | ||||||||||||||||||||||||||||||
|
Subroutine
fh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 439 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 440 | 0 | sysseek($x->{fh}, $x->tell(), SEEK_SET); | |||||||||||||||||||||||||||||||
| 441 | 0 | return $x->{fh}; | |||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | # used only as fail-safe default | ||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | sub _read_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_read_line: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 448 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 449 | return | ||||||||||||||||||||||||||||||||
| 450 | ($x->{move} == 1 ) ? $x->_next_line() : | ||||||||||||||||||||||||||||||||
| 451 | 0 | ▶ | ($x->{move} == -1) ? $x->_prev_line() : | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 452 | undef; | ||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 0 | ▶ | sub _next_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_next_line: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||
| 457 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 458 | # 1. more than 1 line is in the buffer, so the top of the buffer is a | ||||||||||||||||||||||||||||||||
| 459 | # complete line | ||||||||||||||||||||||||||||||||
| 460 | # 2. only line -1 (last line) remains in the buffer | ||||||||||||||||||||||||||||||||
| 461 | # 3. nothing else to read, i.e. return undef | ||||||||||||||||||||||||||||||||
| 462 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | if (@{$x->{buffer}} > 1 || $x->{cur} == $x->{file_size}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@{$x->{"buffer"};} > 1 or $x->{"cur"} == $x->{"file_size"}
MC/DC0%0/2
@{$x->{"buffer"};} > 1 or $x->{"cur"} == $x->{"file_size"}
@{$x->{"buffer"};} > 1
$x->{"cur"} == $x->{"file_size"}
Truth table0%0/3
A or B
A= @{$x->{"buffer"};} > 1B= $x->{"cur"} == $x->{"file_size"}
| |||||||||||||||||||||||||||||||||
| 464 | 0 | my $line = shift @{$x->{buffer}}; | |||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | $x->{line} += $x->{origin} if defined $line; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 466 | 0 | return $line; | |||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||
| 469 | # no complete line, so read something | ||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | # reading forward is easy - just sysseek() to where the bottom of the | ||||||||||||||||||||||||||||||||
| 472 | # buffer is, and let sysread() do the rest | ||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | sysseek($x->{fh}, $x->{cur}, SEEK_SET) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 474 | or croak $!; | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | # sysread returns undef for errors; | ||||||||||||||||||||||||||||||||
| 477 | # due to the pre-condition, 0 should not occur either | ||||||||||||||||||||||||||||||||
| 478 | 0 | my $tmp; | |||||||||||||||||||||||||||||||
| 479 | my $size = sysread($x->{fh}, $tmp, $x->{block_size}) | ||||||||||||||||||||||||||||||||
| 480 | 0 | ▶ | or croak $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 481 | 0 | $x->{cur} += $size; | |||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | # prepend to the temp the leftover partial line in the buffer | ||||||||||||||||||||||||||||||||
| 484 | 0 | $tmp = pop (@{$x->{buffer}}) . $tmp | |||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | if (@{$x->{buffer}}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | # platform-dependent translation | ||||||||||||||||||||||||||||||||
| 488 | $tmp =~ s/$x->{translate}/\n/ | ||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | if defined $x->{translate}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||
| 491 | # split the temp and store it in the buffer | ||||||||||||||||||||||||||||||||
| 492 | 0 | @{$x->{buffer}} = $tmp =~ /$x->{re}/gs; | |||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | sub _prev_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_prev_line: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 497 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 498 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 499 | # 1. more than 1 line is in the buffer, so the bottom of the buffer is | ||||||||||||||||||||||||||||||||
| 500 | # a complete line | ||||||||||||||||||||||||||||||||
| 501 | # 2. only line 1 remains in the buffer | ||||||||||||||||||||||||||||||||
| 502 | # 3. nothing else to read, i.e. return undef | ||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | if (@{$x->{buffer}} > 1 || $x->{cur} == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@{$x->{"buffer"};} > 1 or $x->{"cur"} == 0
MC/DC0%0/2
@{$x->{"buffer"};} > 1 or $x->{"cur"} == 0
@{$x->{"buffer"};} > 1
$x->{"cur"} == 0
Truth table0%0/3
A or B
A= @{$x->{"buffer"};} > 1B= $x->{"cur"} == 0
| |||||||||||||||||||||||||||||||||
| 504 | 0 | my $line = pop @{$x->{buffer}}; | |||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | $x->{line} -= $x->{origin} if defined $line; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 506 | 0 | return $line; | |||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | # no complete line, so read something | ||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||
| 511 | # reading backward requires us to first calculate where the top of the | ||||||||||||||||||||||||||||||||
| 512 | # buffer will reach. be careful to handle trailing bytes properly. | ||||||||||||||||||||||||||||||||
| 513 | 0 | my $read_size = $x->{block_size}; | |||||||||||||||||||||||||||||||
| 514 | 0 | $x->{cur} -= $x->{block_size}; | |||||||||||||||||||||||||||||||
| 515 | 0 | ▶ | if ($x->{cur} < 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 516 | 0 | $read_size += $x->{cur}; | |||||||||||||||||||||||||||||||
| 517 | 0 | $x->{cur} = 0; | |||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||
| 519 | 0 | ▶ | sysseek($x->{fh}, $x->{cur}, SEEK_SET) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 520 | or croak $!; | ||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||
| 522 | # sysread returns undef for errors; | ||||||||||||||||||||||||||||||||
| 523 | # due to the pre-condition, 0 should not occur either | ||||||||||||||||||||||||||||||||
| 524 | 0 | my $tmp = ''; | |||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | sysread($x->{fh}, $tmp, $read_size) == $read_size | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 526 | or croak $!; | ||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||
| 528 | # append to the temp the leftover partial line in the buffer | ||||||||||||||||||||||||||||||||
| 529 | 0 | $tmp .= pop @{$x->{buffer}} | |||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | if (@{$x->{buffer}}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | # platform-dependent translation | ||||||||||||||||||||||||||||||||
| 533 | $tmp =~ s/$x->{translate}/\n/ | ||||||||||||||||||||||||||||||||
| 534 | 0 | ▶ | if defined $x->{translate}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||
| 536 | # split the temp and store it in the buffer | ||||||||||||||||||||||||||||||||
| 537 | 0 | @{$x->{buffer}} = $tmp =~ /$x->{re}/gs; | |||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | # used only as fail-safe default | ||||||||||||||||||||||||||||||||
| 542 | 0 | ▶ | sub _eof { | ||||||||||||||||||||||||||||||
|
Subroutine
_eof: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 543 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 544 | return | ||||||||||||||||||||||||||||||||
| 545 | ($x->{move} == 1 ) ? $x->next_eof() : | ||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | ($x->{move} == -1) ? $x->prev_eof() : | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 547 | undef; | ||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | 0 | ▶ | sub _next_eof { | ||||||||||||||||||||||||||||||
|
Subroutine
_next_eof: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 551 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 552 | 0 | ▶ | return $x->{cur} == $x->{file_size} && @{$x->{buffer}} == 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$x->{'cur'} == $x->{'file_size'} && @{$x->{'buffer'};} == 0
MC/DC0%0/2
$x->{'cur'} == $x->{'file_size'} && @{$x->{'buffer'};} == 0
$x->{'cur'} == $x->{'file_size'}
@{$x->{'buffer'};} == 0
Truth table0%0/3
A && B
A= $x->{'cur'} == $x->{'file_size'}B= @{$x->{'buffer'};} == 0
| |||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | 0 | ▶ | sub _prev_eof { | ||||||||||||||||||||||||||||||
|
Subroutine
_prev_eof: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 556 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 557 | 0 | ▶ | return $x->{cur} == 0 && @{$x->{buffer}} == 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$x->{'cur'} == 0 && @{$x->{'buffer'};} == 0
MC/DC0%0/2
$x->{'cur'} == 0 && @{$x->{'buffer'};} == 0
$x->{'cur'} == 0
@{$x->{'buffer'};} == 0
Truth table0%0/3
A && B
A= $x->{'cur'} == 0B= @{$x->{'buffer'};} == 0
| |||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | # fixes up our aliases so that we eliminate the indirection functions | ||||||||||||||||||||||||||||||||
| 561 | # _read_line() and _eof() | ||||||||||||||||||||||||||||||||
| 562 | 0 | ▶ | sub _fixup_alias { | ||||||||||||||||||||||||||||||
|
Subroutine
_fixup_alias: not called CC=11 SCAR=45.9
| |||||||||||||||||||||||||||||||||
| 563 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | # TODO: walk through the symbol table to do this automatically? | ||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | # redefining aliases | ||||||||||||||||||||||||||||||||
| 568 | 2 | ▶ | no warnings qw/redefine/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | *READLINE = ($x->{move} == 1) ? \&_next_line : ($x->{move} == -1) ? \&_prev_line : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 570 | 0 | ▶ | *getline = ($x->{move} == 1) ? \&_next_line : ($x->{move} == -1) ? \&_prev_line : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 571 | 0 | ▶ | *readline = ($x->{move} == 1) ? \&_next_line : ($x->{move} == -1) ? \&_prev_line : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | *eof = ($x->{move} == 1) ? \&_next_eof : ($x->{move} == -1) ? \&_prev_eof : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 573 | 0 | ▶ | *EOF = ($x->{move} == 1) ? \&_next_eof : ($x->{move} == -1) ? \&_prev_eof : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | # function | ||||||||||||||||||||||||||||||||
| 578 | 0 | ▶ | sub _is_dos { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_dos: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 579 | 0 | return $^O =~ /^(dos|os2|mswin32|cygwin)$/i; | |||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | # function | ||||||||||||||||||||||||||||||||
| 583 | 0 | ▶ | sub _is_mac { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_mac: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 584 | 0 | return $^O =~ /^(macos)$/i; | |||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 0 | ▶ | sub _dump { | ||||||||||||||||||||||||||||||
|
Subroutine
_dump: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 588 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||
| 589 | 0 | require YAML; | |||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||
| 591 | # YAML crashes for regexes | ||||||||||||||||||||||||||||||||
| 592 | 0 | my %h = map {$_ => $x->{$_}} grep {!/^re$/} keys %$x; | |||||||||||||||||||||||||||||||
| 593 | 0 | return YAML::Dump(\%h); | |||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||
| 596 | =pod | ||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | =back | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | =head1 TIED HANDLE INTERFACE | ||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | local *F; | ||||||||||||||||||||||||||||||||
| 603 | tie *F, "File::Bidirectional", $file, {origin => 1} | ||||||||||||||||||||||||||||||||
| 604 | or die $!; | ||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||
| 606 | while (my $line = <F>) { ... } | ||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | (tied *F)->switch(); | ||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||
| 610 | The C<TIEHANDLE>, C<READLINE>, C<EOF>, C<CLOSE> and C<TELL> are aliased to the | ||||||||||||||||||||||||||||||||
| 611 | constructor and the lower-case method names, respectively. All other tied | ||||||||||||||||||||||||||||||||
| 612 | operations, such as seeking and writing, are unsupported and will generate an | ||||||||||||||||||||||||||||||||
| 613 | unknown method area. | ||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||
| 615 | To use the other methods, it is necessary to get at the reference to the object | ||||||||||||||||||||||||||||||||
| 616 | underlying the tied variable via L<perlfunc/tied>. | ||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||
| 618 | =head1 LINE ENDINGS | ||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | Currently, File::Bidirectional attempts to imitate Perl by converting the | ||||||||||||||||||||||||||||||||
| 621 | platform-specific line separator into C<\n>. Currently, this only means | ||||||||||||||||||||||||||||||||
| 622 | converting C<\r> on MacOS, and C<\r\n> on DOS and Windows-type systems (when the | ||||||||||||||||||||||||||||||||
| 623 | C<binmode> option is not set). | ||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||
| 625 | So far, this module has only been tested on Unix where line endings do not need | ||||||||||||||||||||||||||||||||
| 626 | to be converted, thus it will be greatly appreciated if users can feedback | ||||||||||||||||||||||||||||||||
| 627 | whether the line endings conversion work on their respective platforms. | ||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||
| 629 | =head1 BENCHMARKS | ||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||
| 631 | As would be expected, File::Bidirectional is hardly as fast as native Perl I/O. To | ||||||||||||||||||||||||||||||||
| 632 | break the news gently, it can be up to an order of magnitude slower... | ||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||
| 634 | Reading through a 250MB file with various methods yield the following numbers: | ||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||
| 636 | Method | Time (s) | ||||||||||||||||||||||||||||||||
| 637 | -------------------------------------- | ||||||||||||||||||||||||||||||||
| 638 | Native Perl | 5 | ||||||||||||||||||||||||||||||||
| 639 | IO::File | 16 | ||||||||||||||||||||||||||||||||
| 640 | File::Bidirectional (OO) | 42 | ||||||||||||||||||||||||||||||||
| 641 | File::Bidirectional (tied) | 51 | ||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||
| 643 | To be optimistic about it, in the best case File::Bidirectional takes 2.6 times | ||||||||||||||||||||||||||||||||
| 644 | the time taken for IO::File. For smaller files, the absolute time difference | ||||||||||||||||||||||||||||||||
| 645 | may be less noticeable, so you will have to decide if the tradeoff is worth it | ||||||||||||||||||||||||||||||||
| 646 | for your application. It is about as fast as I can make it without dropping | ||||||||||||||||||||||||||||||||
| 647 | down into C, but if anybody has a compelling need for speed or ideas on how to | ||||||||||||||||||||||||||||||||
| 648 | optimize things, please do drop me a line. | ||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||
| 650 | The benchmarks were performed circa 2005, on a Pentium-4 machine with clockspeed | ||||||||||||||||||||||||||||||||
| 651 | 2.8GHz, a 7200rpm IDE harddisk, running Debian sarge and ext3. The programs | ||||||||||||||||||||||||||||||||
| 652 | tested were the respective variants of | ||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||
| 654 | while (my $line = <$fh>) { chomp $line; } | ||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||
| 656 | The record separator was simply C<"\n"> and no newline translation took place. | ||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||
| 658 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||
| 660 | Kian Win Ong, cpan@bulk.squeakyblue.com | ||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||
| 662 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||
| 664 | Copyright (C) 2005 by Kian Win Ong. All rights reserved. This program is free | ||||||||||||||||||||||||||||||||
| 665 | software; you can redistribute it and/or modify it under the same terms as Perl | ||||||||||||||||||||||||||||||||
| 666 | itself. This can be either the GNU General Public License or the Artistic | ||||||||||||||||||||||||||||||||
| 667 | License, as specified in the Perl README file. | ||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||
| 669 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||
| 671 | Thanks goes out to Uri Guttman, the author of File::ReadBackwards, from which I | ||||||||||||||||||||||||||||||||
| 672 | stole a bunch of code and tests. =) | ||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||
| 674 | =cut | ||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||
| 676 | 2 | 1; | |||||||||||||||||||||||||||||||