| 1 | package App::CSVUtils::csv_dump; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 8 | 2 | our $DATE = '2026-07-09'; # DATE | |||||||||||
| 9 | 2 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||
| 10 | 2 | our $VERSION = '1.038'; # VERSION | |||||||||||
| 11 | |||||||||||||
| 12 | 2 | ▶ | use App::CSVUtils qw(gen_csv_util); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | gen_csv_util( | ||||||||||||
| 15 | name => 'csv_dump', | ||||||||||||
| 16 | summary => 'Dump CSV as data structure (array of array/hash)', | ||||||||||||
| 17 | description => <<'MARKDOWN', | ||||||||||||
| 18 | |||||||||||||
| 19 | This utility reads CSV file then dumps it as a text table, or as JSON if you | ||||||||||||
| 20 | specify the `--format=json` or `--json` option. | ||||||||||||
| 21 | |||||||||||||
| 22 | MARKDOWN | ||||||||||||
| 23 | |||||||||||||
| 24 | add_args => { | ||||||||||||
| 25 | hash => { | ||||||||||||
| 26 | summary => 'Dump CSV as array of hashrefs instead of array of arrayrefs', | ||||||||||||
| 27 | schema => 'true*', | ||||||||||||
| 28 | cmdline_aliases => {H=>{}}, | ||||||||||||
| 29 | }, | ||||||||||||
| 30 | }, | ||||||||||||
| 31 | tags => ['category:converting', 'outputs-data-structure'], | ||||||||||||
| 32 | |||||||||||||
| 33 | examples => [ | ||||||||||||
| 34 | { | ||||||||||||
| 35 | summary => 'Convert CSV to a text table', | ||||||||||||
| 36 | argv => ['file.csv'], | ||||||||||||
| 37 | test => 0, | ||||||||||||
| 38 | 'x.doc.show_result' => 0, | ||||||||||||
| 39 | }, | ||||||||||||
| 40 | { | ||||||||||||
| 41 | summary => 'Convert CSV to JSON', | ||||||||||||
| 42 | src => '[[prog]] file.csv --json', | ||||||||||||
| 43 | src_plang => 'bash', | ||||||||||||
| 44 | test => 0, | ||||||||||||
| 45 | 'x.doc.show_result' => 0, | ||||||||||||
| 46 | }, | ||||||||||||
| 47 | ], | ||||||||||||
| 48 | |||||||||||||
| 49 | 8 | ▶ | on_input_header_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 50 | 8 | my $r = shift; | |||||||||||
| 51 | |||||||||||||
| 52 | # this is a key we add ourselves | ||||||||||||
| 53 | 8 | ▶ | $r->{output_rows} //= []; | ||||||||||
|
Condition50%1/2
$r->{"output_rows"} //= []
MC/DC0%0/1
$r->{"output_rows"} //= []
$r->{"output_rows"}
Truth table50%1/2
A
A= $r->{"output_rows"}
| |||||||||||||
| 54 | |||||||||||||
| 55 | 8 | ▶ | if ($r->{util_args}{hash}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 2 | $r->{wants_input_row_as_hashref} = 1; | |||||||||||
| 57 | } else { | ||||||||||||
| 58 | 6 | push @{ $r->{output_rows} }, $r->{input_row}; | |||||||||||
| 59 | } | ||||||||||||
| 60 | }, | ||||||||||||
| 61 | |||||||||||||
| 62 | 25 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 63 | 25 | my $r = shift; | |||||||||||
| 64 | 25 | ▶ | push @{ $r->{output_rows} }, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 25 | $r->{util_args}{hash} ? $r->{input_row_as_hashref} : $r->{input_row}; | |||||||||||
| 66 | }, | ||||||||||||
| 67 | |||||||||||||
| 68 | writes_csv => 0, | ||||||||||||
| 69 | |||||||||||||
| 70 | 8 | ▶ | on_end => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | 8 | my $r = shift; | |||||||||||
| 72 | 8 | $r->{result} = [200, "OK", $r->{output_rows}]; | |||||||||||
| 73 | }, | ||||||||||||
| 74 | 2 | ); | |||||||||||
| 75 | |||||||||||||
| 76 | 2 | 1; | |||||||||||
| 77 | # ABSTRACT: Dump CSV as data structure (array of array/hash) | ||||||||||||
| 78 | |||||||||||||
| 79 | __END__ | ||||||||||||