| 1 | package App::CSVUtils::csv_gen; | ||||||||||||||||||||||||||||||||
| 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 | compile_eval_code | ||||||||||||||||||||||||||||||||
| 15 | 1 | ); | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | gen_csv_util( | ||||||||||||||||||||||||||||||||
| 18 | name => 'csv_gen', | ||||||||||||||||||||||||||||||||
| 19 | summary => 'Generate CSV data using Perl code', | ||||||||||||||||||||||||||||||||
| 20 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | MARKDOWN | ||||||||||||||||||||||||||||||||
| 24 | add_args => { | ||||||||||||||||||||||||||||||||
| 25 | fields => $App::CSVUtils::argspecopt_fields{fields}, | ||||||||||||||||||||||||||||||||
| 26 | eval_fields => { | ||||||||||||||||||||||||||||||||
| 27 | summary => 'Code to generate list of fields', | ||||||||||||||||||||||||||||||||
| 28 | schema => 'str*', | ||||||||||||||||||||||||||||||||
| 29 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | This is an alternative to supplying a static list of fields via `fields` option. | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | Code is expected to return the list of fields as an arrayref. | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | MARKDOWN | ||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||
| 37 | eval => { | ||||||||||||||||||||||||||||||||
| 38 | summary => 'Code to generate row', | ||||||||||||||||||||||||||||||||
| 39 | schema => 'str*', | ||||||||||||||||||||||||||||||||
| 40 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | Code will be compiled in the `main` package. | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | Code is expected to return the row data, which can be a hashref or an arrayref. | ||||||||||||||||||||||||||||||||
| 45 | When code returns false, this signals `csv-gen` to stop the output. Note that | ||||||||||||||||||||||||||||||||
| 46 | you can also limit the number of rows generated by setting the `num_rows` | ||||||||||||||||||||||||||||||||
| 47 | option. | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | MARKDOWN | ||||||||||||||||||||||||||||||||
| 50 | cmdline_aliases => {e=>{}}, | ||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||
| 52 | num_rows => { | ||||||||||||||||||||||||||||||||
| 53 | summary => 'Limit the number of rows to generate', | ||||||||||||||||||||||||||||||||
| 54 | schema => 'uint*', | ||||||||||||||||||||||||||||||||
| 55 | cmdline_aliases => {n=>{}}, | ||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | add_args_rels => { | ||||||||||||||||||||||||||||||||
| 60 | req_one => ['fields', 'eval_fields'], | ||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | reads_csv => 0, | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | tags => ['category:generating', 'accepts-code'], | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | examples => [ | ||||||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||||||
| 69 | summary => 'Generate 10 numbers from 1 to 10', | ||||||||||||||||||||||||||||||||
| 70 | src => q{[[prog]] -f num -n 10 -e '[++$i]'}, | ||||||||||||||||||||||||||||||||
| 71 | src_plang => 'bash', | ||||||||||||||||||||||||||||||||
| 72 | test => 0, | ||||||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||
| 75 | summary => 'Generate 10 numbers from 1 to 10 along with their squares', | ||||||||||||||||||||||||||||||||
| 76 | src => q{[[prog]] -f num -f square -n 10 -e '[++$i, $i*$i]'}, | ||||||||||||||||||||||||||||||||
| 77 | src_plang => 'bash', | ||||||||||||||||||||||||||||||||
| 78 | test => 0, | ||||||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||||||
| 80 | ], | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | after_read_input => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||
| 83 | 0 | my $r = shift; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | # set output fields | ||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($r->{util_args}{eval_fields}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 0 | my $code = compile_eval_code($r->{util_args}{eval_fields}, 'eval_fields'); | |||||||||||||||||||||||||||||||
| 88 | 0 | local $main::r = $r; | |||||||||||||||||||||||||||||||
| 89 | 0 | my $fields = $code->(); | |||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | die [500, "Code in eval_fields did not return list of fields as arranref"] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | unless ref $fields eq 'ArRAY'; | ||||||||||||||||||||||||||||||||
| 92 | 0 | $r->{output_fields} = $fields; | |||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||
| 94 | 0 | $r->{output_fields} = $r->{util_args}{fields}; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | # print rows | ||||||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||||||
| 99 | 0 | my $code = compile_eval_code($r->{util_args}{eval}, 'eval'); | |||||||||||||||||||||||||||||||
| 100 | 0 | local $main::r = $r; | |||||||||||||||||||||||||||||||
| 101 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||
| 102 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 103 | 0 | $i++; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | # we don't use eval_code() because we don't need to provide most | ||||||||||||||||||||||||||||||||
| 106 | # of the variables, because they are not available | ||||||||||||||||||||||||||||||||
| 107 | 0 | local $main::rownum = $i+1; | |||||||||||||||||||||||||||||||
| 108 | 0 | local $main::data_rownum = $i+1; | |||||||||||||||||||||||||||||||
| 109 | 0 | my $row = $code->(); | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | last unless $row; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | last if defined $r->{util_args}{num_rows} && | ||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | ($r->{output_data_rownum}//0) >= $r->{util_args}{num_rows}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $r->{"util_args"}{"num_rows"} and ($r->{"output_data_rownum"} // 0) >= $r->{"util_args"}{"num_rows"}
$r->{"output_data_rownum"} // 0
MC/DC0%0/2
defined $r->{"util_args"}{"num_rows"} and ($r->{"output_data_rownum"} // 0) >= $r->{"util_args"}{"num_rows"}
defined $r->{"util_args"}{"num_rows"}
$r->{"output_data_rownum"}
Truth table0%0/3
A and B
A= defined $r->{"util_args"}{"num_rows"}B= $r->{"output_data_rownum"}
| |||||||||||||||||||||||||||||||||
| 114 | 0 | $r->{code_print_row}->($row); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||||||
| 118 | 1 | ); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 1 | 1; | |||||||||||||||||||||||||||||||
| 121 | # ABSTRACT: Generate CSV data using Perl code | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||