| 1 | package Text::CSV::Piecemeal; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Text::CSV; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | # ABSTRACT: Piecemeal wrapper around Text::CSV for constructing csv files | ||||||||||||
| 8 | |||||||||||||
| 9 | 1 | 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 | 1 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 46 | 1 | my ( $object, $args ) = @_; | |||||||||||
| 47 | 1 | my $self = $args; | |||||||||||
| 48 | 1 | bless $self; | |||||||||||
| 49 | 1 | ▶ | $self->{sep_char} //= ','; | ||||||||||
|
Condition50%1/2
$self->{"sep_char"} //= ","
MC/DC0%0/1
$self->{"sep_char"} //= ","
$self->{"sep_char"}
Truth table50%1/2
A
A= $self->{"sep_char"}
| |||||||||||||
| 50 | 1 | 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 | 3 | ▶ | { | ||||||||||
|
Subroutine
push_value: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 63 | 3 | my ( $self, $value ) = @_; | |||||||||||
| 64 | 3 | ▶ | $self->end_partial_value if $self->{partial_value}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 3 | push @{ $self->{tmp_values} }, $value; | |||||||||||
| 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 | 2 | ▶ | { | ||||||||||
|
Subroutine
push_partial_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 76 | 2 | my ( $self, $value ) = @_; | |||||||||||
| 77 | 2 | $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 | 1 | ▶ | { | ||||||||||
|
Subroutine
end_partial_value: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 88 | 1 | my $self = shift; | |||||||||||
| 89 | 1 | ▶ | if ( $self->{partial_value} ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | { | ||||||||||||
| 91 | 1 | my $value = $self->{partial_value}; | |||||||||||
| 92 | 1 | $self->{partial_value} = ''; | |||||||||||
| 93 | 1 | $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 | 3 | ▶ | { | ||||||||||
|
Subroutine
push_row: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 105 | 3 | my $self = shift; | |||||||||||
| 106 | 3 | my @values = @_; | |||||||||||
| 107 | 3 | ▶ | $self->end_row if defined $self->{tmp_values}->[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 3 | push @{ $self->{rows} }, \@values; | |||||||||||
| 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 | 1 | ▶ | { | ||||||||||
|
Subroutine
end_row: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 119 | 1 | my $self = shift; | |||||||||||
| 120 | 1 | ▶ | $self->end_partial_value if $self->{partial_value}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 1 | ▶ | if ( defined $self->{tmp_values}->[0] ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | { | ||||||||||||
| 123 | 1 | my @values = @{ $self->{tmp_values} }; | |||||||||||
| 124 | 1 | $self->{tmp_values} = []; | |||||||||||
| 125 | 1 | $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 | 1 | ▶ | { | ||||||||||
|
Subroutine
output: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||
| 136 | 1 | my $csv = Text::CSV->new( { sep_char => $self->{sep_char} } ); | |||||||||||
| 137 | 1 | my $result; | |||||||||||
| 138 | 1 | $self->end_row; | |||||||||||
| 139 | 1 | for ( @{ $self->{rows} } ) | |||||||||||
| 140 | { | ||||||||||||
| 141 | 3 | $csv->combine(@$_); | |||||||||||
| 142 | 3 | $result .= $csv->string() . "\n"; | |||||||||||
| 143 | } | ||||||||||||
| 144 | 1 | 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 | 1; | |||||||||||