File Coverage

blib/lib/DBIx/DataModel/Schema/ResultAs/Tsv.pm
Criterion Covered Total %
statement 41 42 97.6
branch 2 6 33.3
condition n/a
subroutine 9 9 100.0
pod 1 2 50.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             #----------------------------------------------------------------------
2             package DBIx::DataModel::Schema::ResultAs::Tsv;
3             #----------------------------------------------------------------------
4 1     1   487 use warnings;
  1         3  
  1         30  
5 1     1   4 use strict;
  1         3  
  1         19  
6 1     1   4 use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)];
  1         2  
  1         7  
7 1     1   84 use Scalar::Util 1.07 qw/openhandle/;
  1         32  
  1         73  
8              
9 1     1   6 use parent 'DBIx::DataModel::Schema::ResultAs';
  1         15  
  1         4  
10              
11 1     1   56 use namespace::clean;
  1         2  
  1         6  
12              
13             sub new {
14 1     1 0 2 my ($class, $file) = @_;
15              
16 1 50       4 croak "-result_as => [Tsv => ...] ... target file is missing" if !$file;
17 1         4 return bless {file => $file}, $class;
18             }
19              
20              
21             sub get_result {
22 1     1 1 2 my ($self, $statement) = @_;
23              
24             # open file
25 1         2 my $fh;
26 1 50       6 if (openhandle $self->{file}) {
27 1         3 $fh = $self->{file};
28             }
29             else {
30             open $fh, ">", $self->{file}
31 0 0       0 or croak "open $self->{file} for writing : $!";
32             }
33              
34             # get data
35 1         3 $statement->execute;
36 1         3 $statement->make_fast;
37              
38             # activate tsv mode by setting output field and record separators
39 1         4 local $\ = "\n";
40 1         2 local $, = "\t";
41              
42             # print header row
43 1     1   381 no warnings 'uninitialized';
  1         2  
  1         183  
44 1         3 my @headers = $statement->headers;
45 1         53 print $fh @headers;
46              
47             # print data rows
48 1         4 while (my $row = $statement->next) {
49 3         5 my @data = @{$row}{@headers};
  3         11  
50 3         10 s/[\t\n]+/ /g foreach @data;
51 3         12 print $fh @data;
52             }
53              
54             # cleanup and return
55 1         49 $statement->finish;
56 1         31 return $self->{file};
57             }
58              
59              
60             1;
61              
62             __END__