| 1 | package App::CSVUtils::csv_split; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 8 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||||||||||||||||||
| 9 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use App::CSVUtils qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | gen_csv_util | ||||||||||||||||||||||||||||
| 14 | 1 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | gen_csv_util( | ||||||||||||||||||||||||||||
| 17 | name => 'csv_split', | ||||||||||||||||||||||||||||
| 18 | summary => 'Split CSV file into several files', | ||||||||||||||||||||||||||||
| 19 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Will output split files xaa, xab, and so on. Each split file will contain a | ||||||||||||||||||||||||||||
| 22 | maximum of `lines` rows (options to limit split files' size based on number of | ||||||||||||||||||||||||||||
| 23 | characters and bytes will be added). Each split file will also contain CSV | ||||||||||||||||||||||||||||
| 24 | header. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Warning: by default, existing split files xaa, xab, and so on will be | ||||||||||||||||||||||||||||
| 27 | overwritten. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Interface is loosely based on the `split` Unix utility. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | MARKDOWN | ||||||||||||||||||||||||||||
| 32 | add_args => { | ||||||||||||||||||||||||||||
| 33 | lines => { | ||||||||||||||||||||||||||||
| 34 | schema => 'posint*', | ||||||||||||||||||||||||||||
| 35 | default => 1000, | ||||||||||||||||||||||||||||
| 36 | cmdline_aliases => {l=>{}}, | ||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | # XXX --bytes (-b) | ||||||||||||||||||||||||||||
| 39 | # XXX --line-bytes (-C) | ||||||||||||||||||||||||||||
| 40 | # XXX -d (numeric suffix) | ||||||||||||||||||||||||||||
| 41 | # --suffix-length (-a) | ||||||||||||||||||||||||||||
| 42 | # --number, -n (chunks) | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | tags => ['category:splitting'], | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | examples => [ | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | summary => 'Split CSV files to xaa, xab, ... where each split file gets 1000 rows', | ||||||||||||||||||||||||||||
| 49 | argv => ['file.csv'], | ||||||||||||||||||||||||||||
| 50 | test => 0, | ||||||||||||||||||||||||||||
| 51 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | ], | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | writes_multiple_csv => 1, | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | before_read_input => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | $r->{output_filenames} = ['xaa']; | |||||||||||||||||||||||||||
| 61 | 0 | $r->{output_num_of_files} = '?'; | |||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 65 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # time to switch to another file? | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($r->{input_data_rownum} > 1 && ($r->{input_data_rownum}+1) % $r->{util_args}{lines} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"input_data_rownum"} > 1 and ($r->{"input_data_rownum"} + 1) % $r->{"util_args"}{"lines"} == 0
MC/DC0%0/2
$r->{"input_data_rownum"} > 1 and ($r->{"input_data_rownum"} + 1) % $r->{"util_args"}{"lines"} == 0
$r->{"input_data_rownum"} > 1
($r->{"input_data_rownum"} + 1) % $r->{"util_args"}{"lines"} == 0
Truth table0%0/3
A and B
A= $r->{"input_data_rownum"} > 1B= ($r->{"input_data_rownum"} + 1) % $r->{"util_args"}{"lines"} == 0
| |||||||||||||||||||||||||||||
| 69 | 0 | $r->{wants_switch_to_next_output_file}++; | |||||||||||||||||||||||||||
| 70 | 0 | my $next_filename = $r->{output_filenames}[-1]; | |||||||||||||||||||||||||||
| 71 | 0 | $next_filename++; | |||||||||||||||||||||||||||
| 72 | 0 | push @{ $r->{output_filenames} }, $next_filename; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $r->{code_print_row}->($r->{input_row}); | |||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | 1 | ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | 1; | |||||||||||||||||||||||||||
| 80 | # ABSTRACT: Split CSV file into several files | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||