File Coverage

blib/lib/Data/Tubes/Plugin/Reader.pm
Criterion Covered Total %
statement 51 51 100.0
branch 9 12 75.0
condition 3 6 50.0
subroutine 12 12 100.0
pod 4 4 100.0
total 79 85 92.9


line stmt bran cond sub pod time code
1             package Data::Tubes::Plugin::Reader;
2 3     3   1216 use strict;
  3         7  
  3         104  
3 3     3   15 use warnings;
  3         6  
  3         103  
4 3     3   14 use English qw< -no_match_vars >;
  3         7  
  3         24  
5             our $VERSION = '0.738';
6              
7 3     3   1165 use Log::Log4perl::Tiny qw< :easy :dead_if_first LOGLEVEL >;
  3         8  
  3         28  
8              
9 3     3   1152 use Data::Tubes::Util qw< normalize_args shorter_sub_names >;
  3         14  
  3         169  
10 3     3   19 use Data::Tubes::Plugin::Util qw< identify >;
  3         5  
  3         1706  
11             my %global_defaults = (
12             input => 'source',
13             output => 'raw',
14             );
15              
16             sub read_by_line {
17 1     1 1 14 return read_by_separator(
18             normalize_args(
19             @_,
20             {
21             name => 'read_by_line',
22             identification => {caller => [caller(0)]},
23             }
24             ),
25             separator => "\n",
26             );
27             } ## end sub read_by_line
28              
29             sub read_by_paragraph {
30 2     2 1 22 return read_by_separator(
31             normalize_args(
32             @_,
33             {
34             name => 'read_by_paragraph',
35             identification => {caller => [caller(0)]},
36             }
37             ),
38             separator => '',
39             );
40             } ## end sub read_by_paragraph
41              
42             sub read_by_record_reader {
43 5     5 1 78 my %args = normalize_args(
44             @_,
45             [
46             {
47             %global_defaults,
48             emit_eof => 0,
49             name => 'read_by_record_reader',
50             identification => {caller => [caller(0)]},
51             },
52             'record_reader'
53             ],
54             );
55 5         31 identify(\%args);
56 5         16 my $name = $args{name};
57              
58 5         9 my $record_reader = $args{record_reader};
59 5 50       15 LOGDIE "$name undefined record_reader" unless defined $record_reader;
60 5 50       22 LOGDIE "$name record_reader MUST be a sub reference"
61             unless ref($record_reader) eq 'CODE';
62              
63 5         10 my $emit_eof = $args{emit_eof};
64 5         8 my $input = $args{input};
65 5   33     22 my $has_input = defined($input) && length($input);
66 5         10 my $output = $args{output};
67             return sub {
68 6     6   13 my $record = shift;
69 6 50       16 my $source = $has_input ? $record->{$input} : $record;
70 6         12 my $fh = $source->{fh};
71              
72             return (
73             iterator => sub {
74 24         52 my $read = $record_reader->($fh);
75 24         103 my $retval = {%$record, $output => $read};
76 24 100       78 return $retval if defined $read;
77 7 100       17 if ($emit_eof) {
78 1         2 $emit_eof = 0;
79 1         6 return $retval;
80             }
81 6         17 return;
82             },
83 6         36 );
84 5         52 };
85             } ## end sub read_by_record_reader
86              
87             sub read_by_separator {
88 5     5 1 53 my %args = normalize_args(
89             @_,
90             [
91             {
92             name => 'read_by_separator',
93             chomp => 1,
94             identification => {caller => [caller(0)]},
95             },
96             'separator'
97             ]
98             );
99 5         21 my $separator = $args{separator};
100 5         10 my $chomp = $args{chomp};
101             return read_by_record_reader(
102             %args,
103             record_reader => sub {
104 24     24   92 my $fh = shift;
105 24         92 local $INPUT_RECORD_SEPARATOR = $separator;
106 24         140 my $retval = <$fh>;
107 24 100 66     147 chomp($retval) if defined($retval) && $chomp;
108 24         77 return $retval;
109             },
110 5         26 );
111             } ## end sub read_by_separator
112              
113             shorter_sub_names(__PACKAGE__, 'read_');
114              
115             1;