| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # BioPerl module for Bio::PullParserI | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | # Cared for by Sendu Bala <bix@sendu.me.uk> | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | # Copyright Sendu Bala | ||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||
| 10 | # You may distribute this module under the same terms as perl itself | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | # POD documentation - main docs before the code | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | Bio::PullParserI - A base module for fast 'pull' parsing | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # do not use this class, it is intended for parser module | ||||||||||||||||||||||||||||||||
| 21 | # writers only | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | If you are writing a module to parse some new format, you may wish to use | ||||||||||||||||||||||||||||||||
| 26 | a 'pull' approach whereby you only do work (reading file data, parsing it, | ||||||||||||||||||||||||||||||||
| 27 | turning the parsed data in an object) when absolutely necessary. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | PullParserI provides a system for doing exactly that. As a PullParser you | ||||||||||||||||||||||||||||||||
| 30 | need a chunk. A chunk is just a Bio::Root::IO that contains all the raw data | ||||||||||||||||||||||||||||||||
| 31 | you would want to parse. You can use the chunk() method to create a chunk from | ||||||||||||||||||||||||||||||||
| 32 | a filename, existing filehandle or even a string. If you make a chunk from a | ||||||||||||||||||||||||||||||||
| 33 | large file, but actually only want your chunk to be some portion of the whole | ||||||||||||||||||||||||||||||||
| 34 | file, supply start and end amounts in bytes to chunk() at the same time. | ||||||||||||||||||||||||||||||||
| 35 | The methods _chunk_seek() and _chunk_tell() provide seeks and tells that are | ||||||||||||||||||||||||||||||||
| 36 | relative to the start and end of your chunk, not the whole file. | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | The other thing you will need to decide when making a chunk is how to handle | ||||||||||||||||||||||||||||||||
| 39 | piped input. A PullParser typically needs seekable data to parse, so if your | ||||||||||||||||||||||||||||||||
| 40 | data is piped in and unseekable, you must decide between creating a temp file | ||||||||||||||||||||||||||||||||
| 41 | or reading the input into memory, which will be done before the chunk becomes | ||||||||||||||||||||||||||||||||
| 42 | usable and you can begin any parsing. Alternatively you can choose to force | ||||||||||||||||||||||||||||||||
| 43 | a sequential read, in which case you can make use of _dependencies() to define | ||||||||||||||||||||||||||||||||
| 44 | the linear order of methods that would result in the file being read | ||||||||||||||||||||||||||||||||
| 45 | sequentially. The return value of _sequential() is also useful here, if you | ||||||||||||||||||||||||||||||||
| 46 | would need to cache some data or otherwise behave differently during a | ||||||||||||||||||||||||||||||||
| 47 | sequential read. | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | The main method in the system is get_field(). This method relies on the | ||||||||||||||||||||||||||||||||
| 50 | existence of a private hash reference accessible to it with the method | ||||||||||||||||||||||||||||||||
| 51 | _fields(). That hash ref should have as keys all the sorts of data you will want | ||||||||||||||||||||||||||||||||
| 52 | to parse (eg. 'score'), and prior to parsing the values would be undefined. A | ||||||||||||||||||||||||||||||||
| 53 | user of your module can then call either $module-E<gt>get_field('score') or | ||||||||||||||||||||||||||||||||
| 54 | $module-E<gt>score and get_field will either return the answer from | ||||||||||||||||||||||||||||||||
| 55 | $self-E<gt>_fields-E<gt>{score} if it is defined, or call a method _discover_score() | ||||||||||||||||||||||||||||||||
| 56 | first if not. So for the system to work you need to define a _discover_*() | ||||||||||||||||||||||||||||||||
| 57 | method for every field in the fields hash, and ensure that the method stores an | ||||||||||||||||||||||||||||||||
| 58 | answer in the fields hash. | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | How you implement your _discover_* methods is up to you, though you should never | ||||||||||||||||||||||||||||||||
| 61 | call a _discover_* method directly yourself; always use get_field(), since | ||||||||||||||||||||||||||||||||
| 62 | get_field() will deal with calling dependent methods for you if a forced | ||||||||||||||||||||||||||||||||
| 63 | sequenctial read is in progress due to piped input. You will almost certainly | ||||||||||||||||||||||||||||||||
| 64 | want to make use of the various chunk-related methods of this class (that are | ||||||||||||||||||||||||||||||||
| 65 | denoted private by the leading '_'; this means you can use them as the author of | ||||||||||||||||||||||||||||||||
| 66 | a parser class, but users of your parser should not). | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | Primary amongst them is _*_chunk_by_end() to which you provide text that | ||||||||||||||||||||||||||||||||
| 69 | represents the end of your desired chunk and it does a readline with your | ||||||||||||||||||||||||||||||||
| 70 | argument as $/. The chunk knows about its line-endings, so if you want your | ||||||||||||||||||||||||||||||||
| 71 | end definition to include a new line, just always use "\n" and PullParserI will | ||||||||||||||||||||||||||||||||
| 72 | do any necessary conversion for you. | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | If your input data is hierarchical (eg. report-E<gt>many results-E<gt>many hits-E<gt>many | ||||||||||||||||||||||||||||||||
| 75 | hsps), and you want an object at the leaf of the hierarchy to have access to | ||||||||||||||||||||||||||||||||
| 76 | information that is shared amongst all of them (is parsed in the root), you | ||||||||||||||||||||||||||||||||
| 77 | don't have to copy the data to each leaf object; simply by defining parent(), | ||||||||||||||||||||||||||||||||
| 78 | when you call get_field() and the requested field isn't in your leaf's fields | ||||||||||||||||||||||||||||||||
| 79 | hash, the leaf's parent will be asked for the field instead, and so on till | ||||||||||||||||||||||||||||||||
| 80 | root. | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | See Bio::SearchIO::hmmer_pull for an example of implementing a parser using | ||||||||||||||||||||||||||||||||
| 83 | PullParserI. | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | =head1 FEEDBACK | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | =head2 Mailing Lists | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | User feedback is an integral part of the evolution of this and other | ||||||||||||||||||||||||||||||||
| 90 | Bioperl modules. Send your comments and suggestions preferably to | ||||||||||||||||||||||||||||||||
| 91 | the Bioperl mailing list. Your participation is much appreciated. | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | bioperl-l@bioperl.org - General discussion | ||||||||||||||||||||||||||||||||
| 94 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =head2 Support | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | Please direct usage questions or support issues to the mailing list: | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | I<bioperl-l@bioperl.org> | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | rather than to the module maintainer directly. Many experienced and | ||||||||||||||||||||||||||||||||
| 103 | reponsive experts will be able look at the problem and quickly | ||||||||||||||||||||||||||||||||
| 104 | address it. Please include a thorough description of the problem | ||||||||||||||||||||||||||||||||
| 105 | with code and data examples if at all possible. | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | =head2 Reporting Bugs | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | Report bugs to the Bioperl bug tracking system to help us keep track | ||||||||||||||||||||||||||||||||
| 110 | of the bugs and their resolution. Bug reports can be submitted via the | ||||||||||||||||||||||||||||||||
| 111 | web: | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | https://github.com/bioperl/bioperl-live/issues | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =head1 AUTHOR - Sendu Bala | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | Email bix@sendu.me.uk | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | Inspired by a posting by Aaron J. Mackey | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | =head1 APPENDIX | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | The rest of the documentation details each of the object methods. | ||||||||||||||||||||||||||||||||
| 126 | Internal methods are usually preceded with a _ | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | # Let the code begin... | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | package Bio::PullParserI; | ||||||||||||||||||||||||||||||||
| 133 | 9 | $Bio::PullParserI::VERSION = '1.7.8'; | |||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | use vars qw($AUTOLOAD $FORCE_TEMP_FILE); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 135 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 9 | ▶ | use Bio::Root::IO; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 9 | ▶ | use base qw(Bio::Root::RootI); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||
| 142 | # chunk() needs perl 5.8 feature for modes other than temp_file, so will | ||||||||||||||||||||||||||||||||
| 143 | # workaround by forcing temp_file mode in <5.8. Could also rewrite using | ||||||||||||||||||||||||||||||||
| 144 | # IO::String, but don't want to. | ||||||||||||||||||||||||||||||||
| 145 | 9 | ▶ | if ($] < 5.008) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 0 | $FORCE_TEMP_FILE = 1; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | =head2 _fields | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | Title : _fields | ||||||||||||||||||||||||||||||||
| 153 | Usage : $obj->_fields( { field1 => undef } ); | ||||||||||||||||||||||||||||||||
| 154 | my $fields_ref = $obj->_fields; | ||||||||||||||||||||||||||||||||
| 155 | Function: Get/set the hash reference containing all the fields for this parser | ||||||||||||||||||||||||||||||||
| 156 | Returns : hash ref | ||||||||||||||||||||||||||||||||
| 157 | Args : none to get, OR hash ref to set | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 12251 | ▶ | sub _fields { | ||||||||||||||||||||||||||||||
|
Subroutine
_fields: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||
| 162 | 12251 | my $self = shift; | |||||||||||||||||||||||||||||||
| 163 | 12251 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 213 | $self->{_fields} = shift; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 12251 | ▶ | unless (defined $self->{_fields}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 0 | $self->{_fields} = { }; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | 12251 | return $self->{_fields}; | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | =head2 has_field | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | Title : has_field | ||||||||||||||||||||||||||||||||
| 175 | Usage : if ($obj->has_field('field_name') {...} | ||||||||||||||||||||||||||||||||
| 176 | Function: Ask if a particular object has a given field (doesn't ask ancestors) | ||||||||||||||||||||||||||||||||
| 177 | Returns : boolean | ||||||||||||||||||||||||||||||||
| 178 | Args : string (the field name to test) | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub has_field { | ||||||||||||||||||||||||||||||
|
Subroutine
has_field: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 183 | 0 | my ($self, $desired) = @_; | |||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | $desired || return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | 0 | return exists $self->_fields->{$desired}; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | =head2 get_field | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | Title : get_field | ||||||||||||||||||||||||||||||||
| 191 | Usage : my $field_value = $obj->get_field('field_name'); | ||||||||||||||||||||||||||||||||
| 192 | Function: Get the value of a given field. If this $obj doesn't have the field, | ||||||||||||||||||||||||||||||||
| 193 | it's parent() will be asked, and so on until there are no more | ||||||||||||||||||||||||||||||||
| 194 | parents. | ||||||||||||||||||||||||||||||||
| 195 | Returns : scalar, warns if a value for the field couldn't be found and returns | ||||||||||||||||||||||||||||||||
| 196 | undef. | ||||||||||||||||||||||||||||||||
| 197 | Args : string (the field to get) | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 2102 | ▶ | sub get_field { | ||||||||||||||||||||||||||||||
|
Subroutine
get_field: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 202 | 2102 | my $self = shift; | |||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $desired = shift || return keys %{$self->_fields}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return keys %{$self->_fields;})
MC/DC0%0/1
shift() || (return keys %{$self->_fields;})
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 204 | 2102 | ▶ | if (exists $self->_fields->{$desired}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 205 | 1972 | ▶ | unless (defined $self->_fields->{$desired}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 206 | 1013 | my $method = '_discover_'.$desired; | |||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 1013 | my $dependency = $self->_dependencies($desired); | |||||||||||||||||||||||||||||||
| 209 | 1013 | ▶ | if ($dependency && ! defined $self->_fields->{$dependency}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dependency and not defined $self->_fields->{$dependency}
MC/DC100%2/2
$dependency and not defined $self->_fields->{$dependency}
$dependency
not defined $self->_fields->{$dependency}
Truth table100%3/3
A and not(B)
A= $dependencyB= not defined $self->_fields->{$dependency}
| |||||||||||||||||||||||||||||||||
| 210 | 451 | $self->get_field($dependency); | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | # it might exist now | ||||||||||||||||||||||||||||||||
| 214 | 1013 | ▶ | $self->$method unless defined $self->_fields->{$desired}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | 1972 | return $self->_fields->{$desired}; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | # is it a field of our parent? (checks all ancestors) | ||||||||||||||||||||||||||||||||
| 220 | 130 | ▶ | if (my $parent = $self->parent) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 129 | return $parent->get_field($desired); | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 1 | $desired =~ s/_discover_//; | |||||||||||||||||||||||||||||||
| 225 | 1 | $self->warn("This report does not hold information about '$desired'"); | |||||||||||||||||||||||||||||||
| 226 | 1 | return; | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | =head2 parent | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | Title : parent | ||||||||||||||||||||||||||||||||
| 232 | Usage : $obj->parent($parent_obj); | ||||||||||||||||||||||||||||||||
| 233 | my $parent_obj = $obj->parent; | ||||||||||||||||||||||||||||||||
| 234 | Function: Get/set the parent object of this one. | ||||||||||||||||||||||||||||||||
| 235 | Returns : Bio::PullParserI | ||||||||||||||||||||||||||||||||
| 236 | Args : none to get, OR Bio::PullParserI to set | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 331 | ▶ | sub parent { | ||||||||||||||||||||||||||||||
|
Subroutine
parent: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 241 | 331 | my $self = shift; | |||||||||||||||||||||||||||||||
| 242 | 331 | ▶ | if (@_) { $self->{parent} = shift } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | 331 | ▶ | return $self->{parent} || return; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'parent'} || (return)
MC/DC100%1/1
$self->{'parent'} || (return)
$self->{'parent'}
Truth table100%2/2
A
A= $self->{'parent'}
| |||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | =head2 chunk | ||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | Title : chunk | ||||||||||||||||||||||||||||||||
| 249 | Usage : $obj->chunk($filename); | ||||||||||||||||||||||||||||||||
| 250 | my $chunk = $obj->chunk; | ||||||||||||||||||||||||||||||||
| 251 | Function: Get/set the chunk of this parser. | ||||||||||||||||||||||||||||||||
| 252 | Returns : Bio:Root::IO | ||||||||||||||||||||||||||||||||
| 253 | Args : none to get, OR | ||||||||||||||||||||||||||||||||
| 254 | First argument of a GLOB reference, filename string, string data to | ||||||||||||||||||||||||||||||||
| 255 | treat as the chunk, or Bio::Root::IO. | ||||||||||||||||||||||||||||||||
| 256 | Optionally, also provide: | ||||||||||||||||||||||||||||||||
| 257 | -start => int : the byte position within the thing described by the | ||||||||||||||||||||||||||||||||
| 258 | first argument to consider as the start of this | ||||||||||||||||||||||||||||||||
| 259 | chunk (default 0) | ||||||||||||||||||||||||||||||||
| 260 | -end => int : the byte position to consider as the end (default | ||||||||||||||||||||||||||||||||
| 261 | true end) | ||||||||||||||||||||||||||||||||
| 262 | -piped_behaviour => 'memory'|'temp_file'|'sequential_read' | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | The last option comes into effect when the first argument is | ||||||||||||||||||||||||||||||||
| 265 | something that cannot be seeked (eg. piped input filehandle). | ||||||||||||||||||||||||||||||||
| 266 | 'memory' means read all the piped input into a string | ||||||||||||||||||||||||||||||||
| 267 | first, then set the chunk to that string. | ||||||||||||||||||||||||||||||||
| 268 | 'temp_file' means read all the piped input and output it to | ||||||||||||||||||||||||||||||||
| 269 | a temp file, then set the chunk to that temp file. | ||||||||||||||||||||||||||||||||
| 270 | 'sequential_read' means that the piped input should be read | ||||||||||||||||||||||||||||||||
| 271 | sequentially and your parsing code must cope with | ||||||||||||||||||||||||||||||||
| 272 | not being able to seek. | ||||||||||||||||||||||||||||||||
| 273 | 'memory' is the fastest but uses the most memory. 'temp_file' and | ||||||||||||||||||||||||||||||||
| 274 | 'sequential_read' can be slow, with 'temp_file' being the most memory | ||||||||||||||||||||||||||||||||
| 275 | efficient but requiring disc space. The default is 'sequential_read'. | ||||||||||||||||||||||||||||||||
| 276 | Note that in versions of perl earlier than 5.8 only temp_file works | ||||||||||||||||||||||||||||||||
| 277 | and will be used regardless of what value is supplied here. | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 6311 | ▶ | sub chunk { | ||||||||||||||||||||||||||||||
|
Subroutine
chunk: called CC=23 SCAR=45.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 282 | 6311 | my $self = shift; | |||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 6311 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 285 | 213 | ▶ | my $thing = shift || $self->throw("Trying to set chunk() to an undefined value"); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->throw('Trying to set chunk() to an undefined value')
MC/DC0%0/2
shift() || $self->throw('Trying to set chunk() to an undefined value')
shift()
$self->throw('Trying to set chunk() to an undefined value')
Truth table33%1/3
A || B
A= shift()B= $self->throw('Trying to set chunk() to an undefined value')
| |||||||||||||||||||||||||||||||||
| 286 | 213 | ▶ | if (ref($thing) eq 'GLOB') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 287 | 0 | $self->{_chunk} = Bio::Root::IO->new(-fh => $thing); | |||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||
| 289 | elsif (ref(\$thing) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||
| 290 | 12 | ▶ | if ($thing !~ /\n/ && -e $thing) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $thing =~ /\n/ and -e $thing
MC/DC0%0/2
not $thing =~ /\n/ and -e $thing
not $thing =~ /\n/
-e $thing
Truth table33%1/3
not(A) and B
A= not $thing =~ /\n/B= -e $thing
| |||||||||||||||||||||||||||||||||
| 291 | 12 | $self->{_chunk} = Bio::Root::IO->new(-file => $thing); | |||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | else { | ||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | unless ($FORCE_TEMP_FILE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 295 | # treat a string as a filehandle | ||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | open my $fake_fh, "+<", \$thing or $self->throw("Could not open file '$thing': $!"); # requires perl 5.8 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | 0 | $self->{_chunk} = Bio::Root::IO->new(-fh => $fake_fh); | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | else { | ||||||||||||||||||||||||||||||||
| 300 | 0 | my ($handle) = $self->{_chunk}->tempfile(); | |||||||||||||||||||||||||||||||
| 301 | 0 | print $handle $thing; | |||||||||||||||||||||||||||||||
| 302 | 0 | $self->{_chunk} = Bio::Root::IO->new(-fh => $handle); | |||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | elsif ($thing->isa('Bio::Root::IO')) { | ||||||||||||||||||||||||||||||||
| 307 | 201 | $self->{_chunk} = $thing; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||||||
| 310 | 0 | $self->throw("Unknown input into chunk()"); | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 213 | my ($piped_behaviour, $start, $end); | |||||||||||||||||||||||||||||||
| 314 | 213 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 315 | 213 | ($piped_behaviour, $start, $end) = | |||||||||||||||||||||||||||||||
| 316 | $self->_rearrange([qw(PIPED_BEHAVIOUR START END)], @_); | ||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | 213 | ▶ | $piped_behaviour ||= 'sequential_read'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$piped_behaviour ||= "sequential_read"
MC/DC100%1/1
$piped_behaviour ||= "sequential_read"
$piped_behaviour
Truth table100%2/2
A
A= $piped_behaviour
| |||||||||||||||||||||||||||||||||
| 319 | 213 | ▶ | $FORCE_TEMP_FILE && ($piped_behaviour = 'temp_file'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | 213 | ▶ | $start ||= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$start ||= 0
MC/DC100%1/1
$start ||= 0
$start
Truth table100%2/2
A
A= $start
| |||||||||||||||||||||||||||||||||
| 321 | 213 | $self->_chunk_true_start($start); | |||||||||||||||||||||||||||||||
| 322 | 213 | $self->_chunk_true_end($end); | |||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | # determine if the chunk is seekable | ||||||||||||||||||||||||||||||||
| 325 | 213 | my $fh = $self->{_chunk}->_fh; | |||||||||||||||||||||||||||||||
| 326 | 213 | seek($fh, 0, 0); | |||||||||||||||||||||||||||||||
| 327 | 213 | my $first_line = <$fh>; | |||||||||||||||||||||||||||||||
| 328 | 213 | seek($fh, 0, 0); | |||||||||||||||||||||||||||||||
| 329 | 213 | my $seekable = tell($fh) == 0; | |||||||||||||||||||||||||||||||
| 330 | 213 | ▶ | unless ($seekable) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | if ($piped_behaviour eq 'memory') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 332 | 0 | my $string = $first_line; | |||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 334 | 0 | $string .= $_; | |||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||
| 336 | 0 | $self->chunk($string); | |||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||
| 338 | elsif ($piped_behaviour eq 'temp_file') { | ||||||||||||||||||||||||||||||||
| 339 | 0 | my ($handle) = $self->{_chunk}->tempfile(); | |||||||||||||||||||||||||||||||
| 340 | 0 | print $handle $first_line; | |||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 342 | 0 | print $handle $_; | |||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | 0 | seek($handle, 0, 0); | |||||||||||||||||||||||||||||||
| 345 | 0 | $self->chunk($handle); | |||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||
| 347 | elsif ($piped_behaviour eq 'sequential_read') { | ||||||||||||||||||||||||||||||||
| 348 | 0 | $self->{_chunk}->_pushback($first_line); | |||||||||||||||||||||||||||||||
| 349 | 0 | $self->_sequential(1); | |||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||
| 351 | else { | ||||||||||||||||||||||||||||||||
| 352 | 0 | $self->throw("Unknown piped behaviour type '$piped_behaviour'"); | |||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | # determine our line ending | ||||||||||||||||||||||||||||||||
| 357 | 213 | ▶ | if ($first_line =~ /\r\n/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 358 | 95 | $self->_line_ending("\r\n"); | |||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||
| 360 | elsif ($first_line =~ /\r/) { | ||||||||||||||||||||||||||||||||
| 361 | 0 | $self->_line_ending("\r"); | |||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||
| 363 | else { | ||||||||||||||||||||||||||||||||
| 364 | 118 | $self->_line_ending("\n"); | |||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | 6311 | ▶ | return $self->{_chunk} || return; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_chunk'} || (return)
MC/DC0%0/1
$self->{'_chunk'} || (return)
$self->{'_chunk'}
Truth table50%1/2
A
A= $self->{'_chunk'}
| |||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | =head2 _sequential | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | Title : _sequential | ||||||||||||||||||||||||||||||||
| 374 | Usage : if ($obj->_sequential) {...} | ||||||||||||||||||||||||||||||||
| 375 | Function: Ask if we have to do operations such that the input is read | ||||||||||||||||||||||||||||||||
| 376 | sequentially. | ||||||||||||||||||||||||||||||||
| 377 | Returns : boolean | ||||||||||||||||||||||||||||||||
| 378 | Args : none to get, OR boolean to set (typically, you should never set this | ||||||||||||||||||||||||||||||||
| 379 | yourself) | ||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | =cut | ||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | 3914 | ▶ | sub _sequential { | ||||||||||||||||||||||||||||||
|
Subroutine
_sequential: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||
| 384 | 3914 | my $self = shift; | |||||||||||||||||||||||||||||||
| 385 | 3914 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 386 | 0 | $self->{_sequential} = shift; | |||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||
| 388 | 3914 | ▶ | return $self->{_sequential} || 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_sequential'} || 0
MC/DC0%0/1
$self->{'_sequential'} || 0
$self->{'_sequential'}
Truth table50%1/2
A
A= $self->{'_sequential'}
| |||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =head2 _dependencies | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | Title : _dependencies | ||||||||||||||||||||||||||||||||
| 394 | Usage : $obj->_dependencies( { field1 => field2 } ); | ||||||||||||||||||||||||||||||||
| 395 | my $dependancy = $obj->_dependencies('field_name'); | ||||||||||||||||||||||||||||||||
| 396 | Function: Set the fields that are dependent on each other, or get the field | ||||||||||||||||||||||||||||||||
| 397 | than another is dependent upon. | ||||||||||||||||||||||||||||||||
| 398 | Returns : string (a field name) | ||||||||||||||||||||||||||||||||
| 399 | Args : string (a field name) to get, OR hash ref to initially set, with | ||||||||||||||||||||||||||||||||
| 400 | field names as keys and values, key field being dependent upon value | ||||||||||||||||||||||||||||||||
| 401 | field. | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | 1226 | ▶ | sub _dependencies { | ||||||||||||||||||||||||||||||
|
Subroutine
_dependencies: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 406 | 1226 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||||||
| 407 | 1226 | ▶ | $thing || return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 408 | 1226 | ▶ | if (ref($thing) eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 409 | 213 | $self->{_dependencies} = $thing; | |||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||
| 411 | else { | ||||||||||||||||||||||||||||||||
| 412 | 1013 | return $self->{_dependencies}->{$thing}; | |||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | =head2 _chunk_true_start | ||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||
| 418 | Title : _chunk_true_start | ||||||||||||||||||||||||||||||||
| 419 | Usage : my $true_start = $obj->_chunk_true_start; | ||||||||||||||||||||||||||||||||
| 420 | Function: Get/set the true start position of the chunk within the filehandle | ||||||||||||||||||||||||||||||||
| 421 | it is part of. | ||||||||||||||||||||||||||||||||
| 422 | Returns : int | ||||||||||||||||||||||||||||||||
| 423 | Args : none to get, OR int to set (typically, you won't set this yourself) | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | =cut | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | 6048 | ▶ | sub _chunk_true_start { | ||||||||||||||||||||||||||||||
|
Subroutine
_chunk_true_start: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 428 | 6048 | my $self = shift; | |||||||||||||||||||||||||||||||
| 429 | 6048 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 430 | 213 | $self->{_chunk_start} = shift; | |||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||
| 432 | 6048 | ▶ | return $self->{_chunk_start} || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'_chunk_start'} || 0
MC/DC100%1/1
$self->{'_chunk_start'} || 0
$self->{'_chunk_start'}
Truth table100%2/2
A
A= $self->{'_chunk_start'}
| |||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | =head2 _chunk_true_end | ||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||
| 437 | Title : _chunk_true_end | ||||||||||||||||||||||||||||||||
| 438 | Usage : my $true_end = $obj->_chunk_true_end; | ||||||||||||||||||||||||||||||||
| 439 | Function: Get/set for the true end position of the chunk within the filehandle | ||||||||||||||||||||||||||||||||
| 440 | it is part of. | ||||||||||||||||||||||||||||||||
| 441 | Returns : int | ||||||||||||||||||||||||||||||||
| 442 | Args : none to get, OR int to set (typically, you won't set this yourself) | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | =cut | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | 3542 | ▶ | sub _chunk_true_end { | ||||||||||||||||||||||||||||||
|
Subroutine
_chunk_true_end: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 447 | 3542 | my $self = shift; | |||||||||||||||||||||||||||||||
| 448 | 3542 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 449 | 213 | $self->{_chunk_end} = shift; | |||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||
| 451 | 3542 | return $self->{_chunk_end}; | |||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | =head2 _line_ending | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | Title : _line_ending | ||||||||||||||||||||||||||||||||
| 457 | Usage : my $line_ending = $obj->_line_ending; | ||||||||||||||||||||||||||||||||
| 458 | Function: Get/set for the line ending for the chunk. | ||||||||||||||||||||||||||||||||
| 459 | Returns : string | ||||||||||||||||||||||||||||||||
| 460 | Args : none to get, OR string to set (typically, you won't set this | ||||||||||||||||||||||||||||||||
| 461 | yourself) | ||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||
| 463 | =cut | ||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | 1282 | ▶ | sub _line_ending { | ||||||||||||||||||||||||||||||
|
Subroutine
_line_ending: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 466 | 1282 | my $self = shift; | |||||||||||||||||||||||||||||||
| 467 | 1282 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 468 | 213 | $self->{_chunk_line_ending} = shift; | |||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||
| 470 | 1282 | return $self->{_chunk_line_ending}; | |||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | =head2 _chunk_seek | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | Title : _chunk_seek | ||||||||||||||||||||||||||||||||
| 476 | Usage : $obj->_chunk_seek($pos); | ||||||||||||||||||||||||||||||||
| 477 | Function: seek() the chunk to the provided position in bytes, relative to the | ||||||||||||||||||||||||||||||||
| 478 | defined start of the chunk within its filehandle. | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | In _sequential() mode, this function does nothing. | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | Returns : n/a | ||||||||||||||||||||||||||||||||
| 483 | Args : int | ||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||
| 485 | =cut | ||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | 895 | ▶ | sub _chunk_seek { | ||||||||||||||||||||||||||||||
|
Subroutine
_chunk_seek: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||
| 488 | 895 | my ($self, $pos) = @_; | |||||||||||||||||||||||||||||||
| 489 | 895 | ▶ | $self->throw("Undefined position passed") unless defined $pos; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 490 | 895 | ▶ | return if $self->_sequential; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | 895 | my $fh = $self->chunk->_fh; | |||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||
| 494 | # seek to the defined start | ||||||||||||||||||||||||||||||||
| 495 | 895 | seek($fh, $self->_chunk_true_start, 0); | |||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | # now seek to desired position relative to defined start | ||||||||||||||||||||||||||||||||
| 498 | 895 | seek($fh, $pos, 1); | |||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | =head2 _chunk_tell | ||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | Title : _chunk_seek | ||||||||||||||||||||||||||||||||
| 504 | Usage : my $pos = $obj->_chunk_tell; | ||||||||||||||||||||||||||||||||
| 505 | Function: Get the current tell() position within the chunk, relative to the | ||||||||||||||||||||||||||||||||
| 506 | defined start of the chunk within its filehandle. | ||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||
| 508 | In _sequential() mode, this function does nothing. | ||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||
| 510 | Returns : int | ||||||||||||||||||||||||||||||||
| 511 | Args : none | ||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | =cut | ||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||
| 515 | 2761 | ▶ | sub _chunk_tell { | ||||||||||||||||||||||||||||||
|
Subroutine
_chunk_tell: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 516 | 2761 | my $self = shift; | |||||||||||||||||||||||||||||||
| 517 | 2761 | ▶ | return if $self->_sequential; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||
| 519 | 2761 | my $fh = $self->chunk->_fh; | |||||||||||||||||||||||||||||||
| 520 | 2761 | return tell($fh) - $self->_chunk_true_start; | |||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||
| 523 | =head2 _get_chunk_by_nol | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | Title : _chunk_seek | ||||||||||||||||||||||||||||||||
| 526 | Usage : my $string = $obj->_get_chunk_by_nol; | ||||||||||||||||||||||||||||||||
| 527 | Function: Get a chunk of chunk() from the current position onward for the given | ||||||||||||||||||||||||||||||||
| 528 | number of lines. | ||||||||||||||||||||||||||||||||
| 529 | Returns : string | ||||||||||||||||||||||||||||||||
| 530 | Args : int (number of lines you want) | ||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | =cut | ||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | 428 | ▶ | sub _get_chunk_by_nol { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_chunk_by_nol: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||||||
| 535 | 428 | my ($self, $nol) = @_; | |||||||||||||||||||||||||||||||
| 536 | 428 | ▶ | $nol > 0 || $self->throw("Can't request a chunk of fewer than 1 lines"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||
| 538 | # hope that $/ is \n | ||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||
| 540 | 428 | my ($line, $count); | |||||||||||||||||||||||||||||||
| 541 | 428 | ▶ | while (defined($_ = $self->chunk->_readline)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 542 | 1159 | $line .= $_; | |||||||||||||||||||||||||||||||
| 543 | 1159 | $count++; | |||||||||||||||||||||||||||||||
| 544 | 1159 | ▶ | last if $count == $nol; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||
| 547 | 428 | my $current = $self->_chunk_tell; | |||||||||||||||||||||||||||||||
| 548 | 428 | ▶ | my $end = ($current || 0) + $self->_chunk_true_start; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$current || 0
MC/DC0%0/1
$current || 0
$current
Truth table50%1/2
A
A= $current
| |||||||||||||||||||||||||||||||||
| 549 | 428 | ▶ | if (! $current || ($self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not $current or $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
MC/DC50%1/2
not $current or $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
not $current
$self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
Truth table66%2/3
not(A) or B
A= not $currentB= $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
| |||||||||||||||||||||||||||||||||
| 550 | 299 | return $line; | |||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||
| 552 | 129 | return; | |||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | =head2 _get_chunk_by_end | ||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||
| 557 | Title : _get_chunk_by_end | ||||||||||||||||||||||||||||||||
| 558 | Usage : my $string = $obj->_get_chunk_by_end; | ||||||||||||||||||||||||||||||||
| 559 | Function: Get a chunk of chunk() from the current position onward till the end | ||||||||||||||||||||||||||||||||
| 560 | of the line, as defined by the supplied argument. | ||||||||||||||||||||||||||||||||
| 561 | Returns : string | ||||||||||||||||||||||||||||||||
| 562 | Args : string (line ending - if you want the line ending to include a new | ||||||||||||||||||||||||||||||||
| 563 | line, always use \n) | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | =cut | ||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | 830 | ▶ | sub _get_chunk_by_end { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_chunk_by_end: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 568 | 830 | my ($self, $chunk_ending) = @_; | |||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||
| 570 | 830 | my $start = $self->_chunk_tell; | |||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||
| 572 | 830 | my $line_ending = $self->_line_ending; | |||||||||||||||||||||||||||||||
| 573 | 830 | $chunk_ending =~ s/\n/$line_ending/g; | |||||||||||||||||||||||||||||||
| 574 | 830 | ▶ | local $/ = $chunk_ending || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$chunk_ending || ''
MC/DC0%0/1
$chunk_ending || ''
$chunk_ending
Truth table50%1/2
A
A= $chunk_ending
| |||||||||||||||||||||||||||||||||
| 575 | 830 | my $line = $self->chunk->_readline; | |||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | 830 | my $current = $self->_chunk_tell; | |||||||||||||||||||||||||||||||
| 578 | 830 | ▶ | my $end = ($current || 0) + $self->_chunk_true_start; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$current || 0
MC/DC100%1/1
$current || 0
$current
Truth table100%2/2
A
A= $current
| |||||||||||||||||||||||||||||||||
| 579 | 830 | ▶ | if (! $current || ($self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $current or $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
MC/DC100%2/2
not $current or $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
not $current
$self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
Truth table100%3/3
not(A) or B
A= not $currentB= $self->_chunk_true_end ? $end <= $self->_chunk_true_end : 1
| |||||||||||||||||||||||||||||||||
| 580 | 578 | return $line; | |||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||
| 583 | 252 | $self->_chunk_seek($start); | |||||||||||||||||||||||||||||||
| 584 | 252 | return; | |||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | =head2 _find_chunk_by_end | ||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||
| 589 | Title : _find_chunk_by_end | ||||||||||||||||||||||||||||||||
| 590 | Usage : my $string = $obj->_find_chunk_by_end; | ||||||||||||||||||||||||||||||||
| 591 | Function: Get the start and end of what would be a chunk of chunk() from the | ||||||||||||||||||||||||||||||||
| 592 | current position onward till the end of the line, as defined by the | ||||||||||||||||||||||||||||||||
| 593 | supplied argument. | ||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||
| 595 | In _sequential() mode, this function does nothing. | ||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||
| 597 | Returns : _chunk_tell values for start and end in 2 element list | ||||||||||||||||||||||||||||||||
| 598 | Args : string (line ending - if you want the line ending to include a new | ||||||||||||||||||||||||||||||||
| 599 | line, always use \n) | ||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||
| 601 | =cut | ||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||
| 603 | 239 | ▶ | sub _find_chunk_by_end { | ||||||||||||||||||||||||||||||
|
Subroutine
_find_chunk_by_end: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 604 | 239 | my ($self, $chunk_ending) = @_; | |||||||||||||||||||||||||||||||
| 605 | 239 | ▶ | return if $self->_sequential; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | 239 | my $line_ending = $self->_line_ending; | |||||||||||||||||||||||||||||||
| 608 | 239 | $chunk_ending =~ s/\n/$line_ending/g; | |||||||||||||||||||||||||||||||
| 609 | 239 | ▶ | local $/ = $chunk_ending || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$chunk_ending || ''
MC/DC0%0/1
$chunk_ending || ''
$chunk_ending
Truth table50%1/2
A
A= $chunk_ending
| |||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||
| 611 | 239 | my $start = $self->_chunk_tell; | |||||||||||||||||||||||||||||||
| 612 | 239 | $self->chunk->_readline; | |||||||||||||||||||||||||||||||
| 613 | 239 | my $end = $self->_chunk_tell; | |||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||
| 615 | 239 | my $comp_end = $end + $self->_chunk_true_start; | |||||||||||||||||||||||||||||||
| 616 | 239 | ▶ | if ($self->_chunk_true_end ? $comp_end <= $self->_chunk_true_end : 1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 617 | 173 | return ($start, $end); | |||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | 66 | $self->_chunk_seek($start); | |||||||||||||||||||||||||||||||
| 621 | 66 | return; | |||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||
| 624 | =head2 AUTOLOAD | ||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||
| 626 | Title : AUTOLOAD | ||||||||||||||||||||||||||||||||
| 627 | Usage : n/a | ||||||||||||||||||||||||||||||||
| 628 | Function: Assumes that any unknown method called should be treated as | ||||||||||||||||||||||||||||||||
| 629 | get_field($method_name). | ||||||||||||||||||||||||||||||||
| 630 | Returns : n/a | ||||||||||||||||||||||||||||||||
| 631 | Args : n/a | ||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||
| 633 | =cut | ||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||
| 635 | 1 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 636 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 637 | 1 | ▶ | ref($self) || return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||
| 639 | 1 | my $name = $AUTOLOAD; | |||||||||||||||||||||||||||||||
| 640 | 1 | $name =~ s/.*://; # strip fully-qualified portion | |||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||
| 642 | # is it one of our fields? | ||||||||||||||||||||||||||||||||
| 643 | 1 | return $self->get_field($name); | |||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | 9 | 1; | |||||||||||||||||||||||||||||||