File Coverage

lib/Text/CSV/Piecemeal.pm
Criterion Covered Total %
statement 46 46 100.0
branch 6 10 60.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 7 7 100.0
total 70 75 93.3


line stmt bran cond sub pod time code
1             package Text::CSV::Piecemeal;
2              
3 1     1   69416 use strict;
  1         2  
  1         27  
4 1     1   5 use warnings;
  1         2  
  1         22  
5 1     1   740 use Text::CSV;
  1         19859  
  1         429  
6              
7             # ABSTRACT: Piecemeal wrapper around Text::CSV for constructing csv files
8              
9             our $VERSION = 'v0.0001';
10              
11            
12             =head1 NAME
13              
14             Text::CSV::Piecemeal
15              
16             =head1 WARNING
17              
18             This module is in early development and may change.
19              
20             =head1 SYNOPSIS
21              
22             $csv = Text::CSV::Piecemeal->new( { sep_char => ',' } );
23              
24             =cut
25              
26             =head1 DESCRIPTION
27              
28             This module provides a simple wrapper around Text::CSV to allow creation of a csv bit by bit.
29              
30             This is a work in progress and contains incomplete test code, methods are likely to be refactored, you have been warned.
31              
32              
33             =head1 METHODS
34              
35             =cut
36              
37             =head2 new( \%args )
38              
39             Create new Piecemeal object takes a hashref of args to pass to Text::CSV ( currently only sep_char )
40              
41             If no args provided defaults are applied:
42             sep_char => ","
43             =cut
44             sub new
45             {
46 1     1 1 82 my ( $object, $args ) = @_;
47 1         2 my $self = $args;
48 1         2 bless $self;
49 1   50     7 $self->{sep_char} //= ',';
50 1         3 return $self;
51             }
52              
53              
54             =head2 push_value( $value )
55              
56             Pushes the provided value to the next cell, will close the previous cell if it has data but has not been closed
57            
58             Closes itself the next operation will be on the next cell, if you need to append to this use push_partial_value instead.
59              
60             =cut
61             sub push_value
62             {
63 3     3 1 7 my ( $self, $value ) = @_;
64 3 100       10 $self->end_partial_value if $self->{partial_value};
65 3         4 push @{ $self->{tmp_values} }, $value;
  3         12  
66             }
67              
68              
69             =head2 push_partial_value( $value )
70              
71             Concatinates the provided value to the current cell
72              
73             =cut
74             sub push_partial_value
75             {
76 2     2 1 5 my ( $self, $value ) = @_;
77 2         8 $self->{partial_value} .= $value;
78             }
79              
80              
81             =head2 end_partial_value
82              
83             Close the current partial value so next operation is on next cell
84              
85             =cut
86             sub end_partial_value
87             {
88 1     1 1 2 my $self = shift;
89 1 50       13 if ( $self->{partial_value} )
90             {
91 1         3 my $value = $self->{partial_value};
92 1         2 $self->{partial_value} = '';
93 1         4 $self->push_value( $value );
94             }
95             }
96              
97              
98             =head2 push_row( @values )
99              
100             Takes an array of values, starts a new row containing these values and closes the row
101              
102             =cut
103             sub push_row
104             {
105 3     3 1 905 my $self = shift;
106 3         10 my @values = @_;
107 3 50       9 $self->end_row if defined $self->{tmp_values}->[0];
108 3         4 push @{ $self->{rows} }, \@values;
  3         15  
109             }
110              
111              
112             =head2 end_row
113              
114             Close the current row, next operation will start a new row.
115              
116             =cut
117             sub end_row
118             {
119 1     1 1 1 my $self = shift;
120 1 50       4 $self->end_partial_value if $self->{partial_value};
121 1 50       3 if ( defined $self->{tmp_values}->[0] )
122             {
123 1         19 my @values = @{ $self->{tmp_values} };
  1         5  
124 1         3 $self->{tmp_values} = [];
125 1         3 $self->push_row( @values );
126             }
127             }
128              
129             =head2 output
130              
131             Converts stored data into csv as a single string and returns it.
132             =cut
133             sub output
134             {
135 1     1 1 2 my $self = shift;
136 1         7 my $csv = Text::CSV->new( { sep_char => $self->{sep_char} } );
137 1         181 my $result;
138 1         4 $self->end_row;
139 1         2 for ( @{ $self->{rows} } )
  1         2  
140             {
141 3         26 $csv->combine(@$_);
142 3         74 $result .= $csv->string() . "\n";
143             }
144 1         17 return $result;
145             }
146              
147             =head1 SOURCE CODE
148              
149             The source code for this module is held in a public git repository on Gitlab https://gitlab.com/rnewsham/text-csv-piecemeal
150              
151             =head1 LICENSE AND COPYRIGHT
152            
153             Copyright (c) 2018 Richard Newsham
154            
155             This library is free software; you can redistribute it and/or
156             modify it under the same terms as Perl itself.
157            
158             =head1 BUGS AND LIMITATIONS
159            
160             See rt.cpan.org for current bugs, if any.
161            
162             =head1 INCOMPATIBILITIES
163            
164             None known.
165            
166             =head1 DEPENDENCIES
167              
168             Text::CSV
169              
170             =cut
171              
172             1;