File Coverage

blib/lib/ETL/Yertl/Command/yfrom.pm
Criterion Covered Total %
statement 27 29 93.1
branch 8 10 80.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 39 44 88.6


line stmt bran cond sub pod time code
1             package ETL::Yertl::Command::yfrom;
2             our $VERSION = '0.035';
3             # ABSTRACT: Read documents from a format like JSON or CSV
4              
5 1     1   6 use ETL::Yertl;
  1         3  
  1         9  
6 1     1   279 use ETL::Yertl::Util qw( load_module );
  1         2  
  1         43  
7 1     1   6 use Module::Runtime qw( use_module compose_module_name );
  1         1  
  1         4  
8              
9             sub main {
10 4     4 0 9 my $class = shift;
11              
12 4         6 my %opt;
13 4 50       14 if ( ref $_[-1] eq 'HASH' ) {
14 4         6 %opt = %{ pop @_ };
  4         9  
15             }
16              
17 4         10 my ( $format, @files ) = @_;
18              
19 4 100       13 die "Must give a format\n" unless $format;
20 3         11 my $formatter_class = load_module( format => $format );
21 2         5 my $out_formatter = load_module( format => "default" )->new;
22              
23 2 100       7 push @files, "-" unless @files;
24 2         5 for my $file ( @files ) {
25              
26             # We're doing a similar behavior to <>, but manually for easier testing.
27 2         3 my $fh;
28 2 100       7 if ( $file eq '-' ) {
29             # Use the existing STDIN so tests can fake it
30 1         3 $fh = \*STDIN;
31             }
32             else {
33 1 50       12 unless ( open $fh, '<', $file ) {
34 0         0 warn "Could not open file '$file' for reading: $!\n";
35 0         0 next;
36             }
37             }
38              
39 2         42 my $in_formatter = $formatter_class->new( input => $fh, %opt );
40 2         7 my @docs = $in_formatter->read;
41 2         12 print $out_formatter->write( @docs );
42             }
43             }
44              
45             1;
46              
47             __END__