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   677 use warnings;
  1         2  
  1         36  
5 1     1   6 use strict;
  1         3  
  1         26  
6 1     1   6 use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)];
  1         2  
  1         8  
7 1     1   116 use Scalar::Util 1.07 qw/openhandle/;
  1         35  
  1         80  
8              
9 1     1   8 use parent 'DBIx::DataModel::Schema::ResultAs';
  1         3  
  1         5  
10              
11 1     1   64 use namespace::clean;
  1         2  
  1         26  
12              
13             sub new {
14 1     1 0 4 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         3 my $fh;
26 1 50       9 if (openhandle $self->{file}) {
27 1         2 $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         5 $statement->execute;
36 1         5 $statement->make_fast;
37              
38             # activate tsv mode by setting output field and record separators
39 1         5 local $\ = "\n";
40 1         3 local $, = "\t";
41              
42             # print header row
43 1     1   519 no warnings 'uninitialized';
  1         2  
  1         214  
44 1         3 my @headers = $statement->headers;
45 1         29 print $fh @headers;
46              
47             # print data rows
48 1         4 while (my $row = $statement->next) {
49 3         6 my @data = @{$row}{@headers};
  3         8  
50 3         17 s/[\t\n]+/ /g foreach @data;
51 3         15 print $fh @data;
52             }
53              
54             # cleanup and return
55 1         66 $statement->finish;
56 1         48 return $self->{file};
57             }
58              
59              
60             1;
61              
62             __END__