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.036';
3             # ABSTRACT: Read documents from a format like JSON or CSV
4              
5 1     1   7 use ETL::Yertl;
  1         2  
  1         10  
6 1     1   49 use ETL::Yertl::Util qw( load_module );
  1         4  
  1         48  
7 1     1   5 use Module::Runtime qw( use_module compose_module_name );
  1         2  
  1         5  
8              
9             sub main {
10 4     4 0 10 my $class = shift;
11              
12 4         5 my %opt;
13 4 50       14 if ( ref $_[-1] eq 'HASH' ) {
14 4         8 %opt = %{ pop @_ };
  4         11  
15             }
16              
17 4         29 my ( $format, @files ) = @_;
18              
19 4 100       16 die "Must give a format\n" unless $format;
20 3         13 my $formatter_class = load_module( format => $format );
21 2         8 my $out_formatter = load_module( format => "default" )->new;
22              
23 2 100       8 push @files, "-" unless @files;
24 2         7 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       9 if ( $file eq '-' ) {
29             # Use the existing STDIN so tests can fake it
30 1         5 $fh = \*STDIN;
31             }
32             else {
33 1 50       11 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         46 my $in_formatter = $formatter_class->new( input => $fh, %opt );
40 2         10 my @docs = $in_formatter->read;
41 2         14 print $out_formatter->write( @docs );
42             }
43             }
44              
45             1;
46              
47             __END__