| 1 | # IO/File/RecordStream.pm | ||||||||||||||
| 2 | package IO::File::RecordStream; | ||||||||||||||
| 3 | 4 | our $VERSION = '0.05'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 4 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use MooseX::StrictConstructor; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | ▶ | use autodie qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use Scalar::Util qw(reftype openhandle); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 4 | ▶ | use IO::Lines; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 4 | has 'file_name' => ( | |||||||||||||
| 19 | is => 'ro', | ||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||
| 21 | predicate => 'has_file_name', | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 4 | has 'file_handle' => ( | |||||||||||||
| 25 | is => 'ro', | ||||||||||||||
| 26 | isa => 'FileHandle', | ||||||||||||||
| 27 | builder => '_build_file_handle', | ||||||||||||||
| 28 | lazy => 1, | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 4 | has 'end_reached' => ( | |||||||||||||
| 32 | is => 'ro', | ||||||||||||||
| 33 | default => 0, | ||||||||||||||
| 34 | init_arg => undef, | ||||||||||||||
| 35 | writer => '_end_reached', # private writer | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | |||||||||||||||
| 38 | # A regexp matching the separator line used to separate individual records | ||||||||||||||
| 39 | 4 | has 'match_separator' => ( | |||||||||||||
| 40 | is => 'ro', | ||||||||||||||
| 41 | isa => 'RegexpRef', | ||||||||||||||
| 42 | required => 1, | ||||||||||||||
| 43 | ); | ||||||||||||||
| 44 | |||||||||||||||
| 45 | # A code ref that can be passed a ref to the array containing the read | ||||||||||||||
| 46 | # lines and that makes a new record object from it. | ||||||||||||||
| 47 | 4 | has '_record_factory' => ( # keep the ref private | |||||||||||||
| 48 | is => 'ro', | ||||||||||||||
| 49 | isa => 'CodeRef', | ||||||||||||||
| 50 | init_arg => 'record_factory', | ||||||||||||||
| 51 | required => 1, | ||||||||||||||
| 52 | ); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | # Allow various calling styles of the constructor: | ||||||||||||||
| 55 | # new(file_handle): pass file handle to read data from | ||||||||||||||
| 56 | # new(file_name): pass file name of file to read data from | ||||||||||||||
| 57 | # These don't make much sense in this class because other attributes require | ||||||||||||||
| 58 | # initialization as well, but in a sub-class these may be overwritten and | ||||||||||||||
| 59 | # calling with only a file name and file handle is convenient. | ||||||||||||||
| 60 | 3 | ▶ | around BUILDARGS => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||
| 61 | 3 | my $orig = shift; | |||||||||||||
| 62 | 3 | my $class = shift; | |||||||||||||
| 63 | |||||||||||||||
| 64 | 3 | ▶ | return $class->$orig(@_) unless @_ == 1; # no special handling | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | # Check if we got a file name or handle for multi-record input file. | ||||||||||||||
| 67 | 3 | ▶ | if (not reftype $_[0]) { # file name given | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | 1 | my $input_file_name = shift; | |||||||||||||
| 69 | 1 | return $class->$orig(file_name => $input_file_name); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | elsif (reftype $_[0] eq reftype \*STDIN) { # file handle given | ||||||||||||||
| 72 | 2 | my $input_file_handle = shift; | |||||||||||||
| 73 | 2 | return $class->$orig(file_handle => $input_file_handle); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | else { # no file name / handle | ||||||||||||||
| 76 | 0 | return $class->$orig(@_); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 4 | }; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 3 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||
| 81 | 3 | my $self = shift; | |||||||||||||
| 82 | |||||||||||||||
| 83 | 3 | ▶ | confess 'The value of file_handle does not seem to be an open handle' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | unless openhandle $self->file_handle; | ||||||||||||||
| 85 | |||||||||||||||
| 86 | # If the input file is empty, set end_reached immediately. | ||||||||||||||
| 87 | 3 | ▶ | $self->_end_reached(1) if eof $self->file_handle; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | 3 | return; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | # Open file handle from file name if no handle was passed. Die if we cant. | ||||||||||||||
| 93 | 1 | ▶ | sub _build_file_handle { | ||||||||||||
|
Subroutine
_build_file_handle: called CC=2 SCAR=7.0
| |||||||||||||||
| 94 | 1 | my $self = shift; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 1 | ▶ | confess 'Cannot build file handle unless a file name was specified' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | unless $self->has_file_name; | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 1 | open my $input_file_handle, '<', $self->file_name; | |||||||||||||
| 100 | 1 | return $input_file_handle; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | # Returns chomped lines. | ||||||||||||||
| 104 | 4 | ▶ | sub _read_next_record { | ||||||||||||
|
Subroutine
_read_next_record: called CC=4 SCAR=14.0
| |||||||||||||||
| 105 | 4 | my $self = shift; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 4 | my $record_file_handle = $self->file_handle; | |||||||||||||
| 108 | 4 | my @record_lines; | |||||||||||||
| 109 | 4 | ▶ | while (<$record_file_handle>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 140 | my $line = $_; | |||||||||||||
| 111 | 140 | chomp $line; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 140 | ▶ | if ($line =~ $self->match_separator) { # end of record | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | # Drop separator line and return current collection of lines. | ||||||||||||||
| 115 | # Also test if file ends in a separator. | ||||||||||||||
| 116 | 4 | ▶ | $self->_end_reached(1) if eof $record_file_handle; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | return \@record_lines | ||||||||||||||
| 118 | 4 | } | |||||||||||||
| 119 | else { | ||||||||||||||
| 120 | # Store lines until end of record | ||||||||||||||
| 121 | 136 | push @record_lines, $line; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 0 | $self->_end_reached(1); # file has been read | |||||||||||||
| 126 | 0 | return \@record_lines; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | # Get the next record from the multi-record file. | ||||||||||||||
| 130 | 5 | ▶ | sub next { | ||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 131 | 5 | my $self = shift; | |||||||||||||
| 132 | |||||||||||||||
| 133 | # Are there any more entries? | ||||||||||||||
| 134 | 5 | ▶ | return if $self->end_reached; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | |||||||||||||||
| 136 | # Read lines of next record. | ||||||||||||||
| 137 | 4 | my $record_lines_ref = $self->_read_next_record; | |||||||||||||
| 138 | |||||||||||||||
| 139 | # Construct record object using factory | ||||||||||||||
| 140 | 4 | my $record_array_handle = IO::Lines->new($record_lines_ref); | |||||||||||||
| 141 | 4 | my $record = $self->_record_factory->($record_array_handle); | |||||||||||||
| 142 | |||||||||||||||
| 143 | 4 | return $record; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 4 | 1; # End of IO::File::RecordStream | |||||||||||||
| 149 | |||||||||||||||
| 150 | __END__ | ||||||||||||||