| 1 | package File::Record; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | File::Record - Read file by record! | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||
| 13 | |||||||||||||||
| 14 | Version 0.01 | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =cut | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | our $VERSION = '0.01'; | |||||||||||||
| 19 | |||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||
| 22 | |||||||||||||||
| 23 | use File::Record; | ||||||||||||||
| 24 | |||||||||||||||
| 25 | my $reader = File::Record->new( | ||||||||||||||
| 26 | mode => 'start', | ||||||||||||||
| 27 | filehandle => $fh, | ||||||||||||||
| 28 | pattern => qr/^\S/ | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | or | ||||||||||||||
| 32 | |||||||||||||||
| 33 | my $reader = File::Record->new(); | ||||||||||||||
| 34 | $reader->mode('start'); | ||||||||||||||
| 35 | $reader->filehandle('$fh'); | ||||||||||||||
| 36 | $reader->pattern(qr/^\S/); | ||||||||||||||
| 37 | |||||||||||||||
| 38 | while (my $record = $reader->next_record()){ | ||||||||||||||
| 39 | ... | ||||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||
| 43 | |||||||||||||||
| 44 | Usually the file we are working on contains many records, which consist of | ||||||||||||||
| 45 | several line, and we want to read the file record by record instead of line by | ||||||||||||||
| 46 | line. For record with explicit terminator, we can easily do that by set the | ||||||||||||||
| 47 | input record separator (C<$/>) to that terminator. But for record with no | ||||||||||||||
| 48 | explicit terminator, we can only do that by testing the next line. | ||||||||||||||
| 49 | |||||||||||||||
| 50 | This module allows you to define the record and read the file record by record. | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head2 new | ||||||||||||||
| 55 | |||||||||||||||
| 56 | The constructor takes key/value pairs to set the behavior of file reader to be | ||||||||||||||
| 57 | created. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | #Defalt simulate the diamond operator <>. | ||||||||||||||
| 60 | my $reader = File::Record->new(); | ||||||||||||||
| 61 | while ($reader->next_record){ | ||||||||||||||
| 62 | ... | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | equal to | ||||||||||||||
| 66 | |||||||||||||||
| 67 | while (<>){ | ||||||||||||||
| 68 | ... | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | #Specify the pattern to identify the marker line | ||||||||||||||
| 72 | my $reader = File::Record->new(pattern => "^\t"); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | To identify the marker line of a record, a regular expression is needed. | ||||||||||||||
| 75 | By default, the C<$/> is used to simulate the diamond operator E<lt>E<gt>. | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =cut | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=8 SCAR=24.9
Pod
documented
| |||||||||||||||
| 80 | 1 | ▶ | ref ( my $class = shift ) and croak "class name needed!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 1 | ▶ | croak "Odd number of arguments\n" if (@_ % 2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 1 | my %arg = @_; | |||||||||||||
| 83 | 1 | my $self; | |||||||||||||
| 84 | 1 | my @options = qw(mode filehandle pattern); | |||||||||||||
| 85 | 1 | foreach my $key ( keys %arg ){ | |||||||||||||
| 86 | 0 | ▶ | croak "Unknown option $key" unless grep { $_ eq $key } @options; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | $self->{$key} = $arg{$key}; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 1 | ▶ | $self->{mode} or $self->{mode} = $class->default_mode; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 1 | ▶ | $self->{filehandle} or $self->{filehandle} = $class->default_filehandle; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 1 | ▶ | $self->{pattern} or $self->{pattern} = $class->default_pattern; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 1 | bless $self, $class; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head2 next_record | ||||||||||||||
| 96 | |||||||||||||||
| 97 | my $record = $reader->next_record(); | ||||||||||||||
| 98 | |||||||||||||||
| 99 | Return one C<$record> based on current C<$reader>. | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =cut | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | ▶ | sub next_record { | ||||||||||||
|
Subroutine
next_record: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||
| 104 | 0 | ▶ | ref ( my $self = shift ) or croak "instance variable needed"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | my $fh = $self->filehandle; | |||||||||||||
| 106 | # return undef if eof($fh); | ||||||||||||||
| 107 | 0 | my $record; | |||||||||||||
| 108 | 0 | my $pattern = $self->pattern; | |||||||||||||
| 109 | 0 | my $mode = $self->mode; | |||||||||||||
| 110 | 0 | ▶ | if ($mode eq "end"){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 111 | 0 | ▶ | while (<$fh>){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 0 | $record .= $_; | |||||||||||||
| 113 | 0 | ▶ | return $record if /$pattern/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | } | ||||||||||||||
| 115 | } elsif ( $mode eq "start" ){ | ||||||||||||||
| 116 | 0 | my $previous_line_offset; | |||||||||||||
| 117 | 0 | ▶ | while (<$fh>){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | my $current_line_offset = tell $fh; | |||||||||||||
| 119 | 0 | ▶ | if ( /$pattern/ ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | ▶ | if ( $record ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 0 | seek $fh, $previous_line_offset, 0; | |||||||||||||
| 122 | 0 | return $record; | |||||||||||||
| 123 | } else { | ||||||||||||||
| 124 | 0 | $record .= $_; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | } else { | ||||||||||||||
| 127 | 0 | $record .= $_; | |||||||||||||
| 128 | 0 | ▶ | if ( eof($fh) ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 0 | return $record; | |||||||||||||
| 130 | } else { | ||||||||||||||
| 131 | 0 | $previous_line_offset = $current_line_offset; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | } | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head2 mode | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =cut | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 1 | ▶ | sub mode { | ||||||||||||
|
Subroutine
mode: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||
| 143 | 1 | ▶ | ref( my $self = shift ) or croak "instance variable is needed"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 1 | ▶ | @_ ? $self->{mode} = shift : $self->{mode}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 default_mode | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =cut | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 1 | ▶ | sub default_mode { | ||||||||||||
|
Subroutine
default_mode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 152 | 1 | "end" | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head2 pattern | ||||||||||||||
| 156 | |||||||||||||||
| 157 | my $pattern = $reader->pattern; | ||||||||||||||
| 158 | $reader->pattern("^\t"); | ||||||||||||||
| 159 | |||||||||||||||
| 160 | Getter/setter for pattern. | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =cut | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 1 | ▶ | sub pattern { | ||||||||||||
|
Subroutine
pattern: called CC=4 SCAR=19.7
Pod
documented
| |||||||||||||||
| 165 | 1 | ▶ | ref( my $self = shift ) or croak "instance variable is needed"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 1 | ▶ | if ( @_ ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 0 | my $pattern = shift; | |||||||||||||
| 168 | 0 | ▶ | $pattern = qr/\Q$pattern\E/ unless ref $pattern eq 'Regexp'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | $self->{pattern} = $pattern; | |||||||||||||
| 170 | } else { | ||||||||||||||
| 171 | $self->{pattern} | ||||||||||||||
| 172 | 1 | } | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 default_pattern | ||||||||||||||
| 176 | |||||||||||||||
| 177 | =cut | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 1 | ▶ | sub default_pattern { | ||||||||||||
|
Subroutine
default_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 180 | 1 | qr/\n$/; | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =head2 filehandle | ||||||||||||||
| 184 | |||||||||||||||
| 185 | my $fh = $reader->filehandle; | ||||||||||||||
| 186 | $reader->filehandle($fh); | ||||||||||||||
| 187 | |||||||||||||||
| 188 | Getter/setter for filehandle. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =cut | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 0 | ▶ | sub filehandle { | ||||||||||||
|
Subroutine
filehandle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 193 | 0 | ▶ | ref( my $self = shift ) or croak "instance variable needed"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 194 | 0 | ▶ | @_ ? $self->{filehandle} = shift : $self->{filehandle}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | } | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =head2 default_filehandle | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =cut | ||||||||||||||
| 200 | |||||||||||||||
| 201 | 1 | ▶ | sub default_filehandle { | ||||||||||||
|
Subroutine
default_filehandle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 202 | 1 | \*ARGV | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||
| 206 | |||||||||||||||
| 207 | freedog, C<< <freedog at cpan.org> >> | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =head1 BUGS | ||||||||||||||
| 210 | |||||||||||||||
| 211 | Please report any bugs or feature requests to C<bug-file-record at rt.cpan.org>, or through | ||||||||||||||
| 212 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Record>. I will be notified, and then you'll | ||||||||||||||
| 213 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||
| 214 | |||||||||||||||
| 215 | |||||||||||||||
| 216 | |||||||||||||||
| 217 | |||||||||||||||
| 218 | =head1 SUPPORT | ||||||||||||||
| 219 | |||||||||||||||
| 220 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 221 | |||||||||||||||
| 222 | perldoc File::Record | ||||||||||||||
| 223 | |||||||||||||||
| 224 | |||||||||||||||
| 225 | You can also look for information at: | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =over 4 | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||
| 230 | |||||||||||||||
| 231 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Record> | ||||||||||||||
| 232 | |||||||||||||||
| 233 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 234 | |||||||||||||||
| 235 | L<http://annocpan.org/dist/File-Record> | ||||||||||||||
| 236 | |||||||||||||||
| 237 | =item * CPAN Ratings | ||||||||||||||
| 238 | |||||||||||||||
| 239 | L<https://cpanratings.perl.org/d/File-Record> | ||||||||||||||
| 240 | |||||||||||||||
| 241 | =item * Search CPAN | ||||||||||||||
| 242 | |||||||||||||||
| 243 | L<https://metacpan.org/release/File-Record> | ||||||||||||||
| 244 | |||||||||||||||
| 245 | =back | ||||||||||||||
| 246 | |||||||||||||||
| 247 | |||||||||||||||
| 248 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 249 | |||||||||||||||
| 250 | |||||||||||||||
| 251 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 252 | |||||||||||||||
| 253 | Copyright 2019 freedog. | ||||||||||||||
| 254 | |||||||||||||||
| 255 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 256 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||
| 257 | copy of the full license at: | ||||||||||||||
| 258 | |||||||||||||||
| 259 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||
| 260 | |||||||||||||||
| 261 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||
| 262 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||
| 263 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||
| 264 | or distribute the Package, if you do not accept this license. | ||||||||||||||
| 265 | |||||||||||||||
| 266 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||
| 267 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||
| 268 | your Modified Version complies with the requirements of this license. | ||||||||||||||
| 269 | |||||||||||||||
| 270 | This license does not grant you the right to use any trademark, service | ||||||||||||||
| 271 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||
| 272 | |||||||||||||||
| 273 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||
| 274 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||
| 275 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||
| 276 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||
| 277 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||
| 278 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||
| 279 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||
| 280 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||
| 281 | |||||||||||||||
| 282 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||
| 283 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||
| 284 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||
| 285 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||
| 286 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||
| 287 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||
| 288 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||
| 289 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||
| 290 | |||||||||||||||
| 291 | |||||||||||||||
| 292 | =cut | ||||||||||||||
| 293 | |||||||||||||||
| 294 | 2 | 1; # End of File::Record | |||||||||||||