| 1 | package App::CSVUtils::csv2vcf; | ||||||||||||||||||||||||||||
| 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 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 9 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||||||||||||||||||
| 10 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use App::CSVUtils qw(gen_csv_util); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | gen_csv_util( | ||||||||||||||||||||||||||||
| 16 | name => 'csv2vcf', | ||||||||||||||||||||||||||||
| 17 | summary => 'Create a VCF from selected fields of the CSV', | ||||||||||||||||||||||||||||
| 18 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | You can set which CSV fields to use for name, cell phone, and email. If unset, | ||||||||||||||||||||||||||||
| 21 | will guess from the field name. If that also fails, will warn/bail out. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | MARKDOWN | ||||||||||||||||||||||||||||
| 24 | add_args => { | ||||||||||||||||||||||||||||
| 25 | name_vcf_field => { | ||||||||||||||||||||||||||||
| 26 | summary => 'Select field to use as VCF N (name) field', | ||||||||||||||||||||||||||||
| 27 | schema => 'str*', | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | cell_vcf_field => { | ||||||||||||||||||||||||||||
| 30 | summary => 'Select field to use as VCF CELL field', | ||||||||||||||||||||||||||||
| 31 | schema => 'str*', | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | email_vcf_field => { | ||||||||||||||||||||||||||||
| 34 | summary => 'Select field to use as VCF EMAIL field', | ||||||||||||||||||||||||||||
| 35 | schema => 'str*', | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | tags => ['category:converting', 'format:vcf'], | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | examples => [ | ||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | summary => 'Create an addressbook from CSV', | ||||||||||||||||||||||||||||
| 43 | argv => ['addressbook.csv'], | ||||||||||||||||||||||||||||
| 44 | test => 0, | ||||||||||||||||||||||||||||
| 45 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||
| 47 | ], | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | writes_csv => 0, | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | on_begin => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 53 | 0 | $r->{wants_input_row_as_hashref}++; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # this is the key we add to the stash | ||||||||||||||||||||||||||||
| 56 | 0 | $r->{vcf} = ''; | |||||||||||||||||||||||||||
| 57 | 0 | $r->{fields_for} = {}; | |||||||||||||||||||||||||||
| 58 | 0 | $r->{fields_for}{N} = $r->{util_args}{name_vcf_field}; | |||||||||||||||||||||||||||
| 59 | 0 | $r->{fields_for}{CELL} = $r->{util_args}{cell_vcf_field}; | |||||||||||||||||||||||||||
| 60 | 0 | $r->{fields_for}{EMAIL} = $r->{util_args}{email_vcf_field}; | |||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | on_input_header_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 64 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | for my $field (@{ $r->{input_fields} }) { | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($field =~ /name|nama/i && !defined($r->{fields_for}{N})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$field =~ /name|nama/i and not defined $r->{"fields_for"}{"N"}
MC/DC0%0/2
$field =~ /name|nama/i and not defined $r->{"fields_for"}{"N"}
$field =~ /name|nama/i
not defined $r->{"fields_for"}{"N"}
Truth table0%0/3
A and not(B)
A= $field =~ /name|nama/iB= not defined $r->{"fields_for"}{"N"}
| |||||||||||||||||||||||||||||
| 68 | 0 | log_info "Will be using field '$field' for VCF field 'N' (name)"; | |||||||||||||||||||||||||||
| 69 | 0 | $r->{fields_for}{N} = $field; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($field =~ /(e-?)?mail/i && !defined($r->{fields_for}{EMAIL})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$field =~ /(e-?)?mail/i and not defined $r->{"fields_for"}{"EMAIL"}
MC/DC0%0/2
$field =~ /(e-?)?mail/i and not defined $r->{"fields_for"}{"EMAIL"}
$field =~ /(e-?)?mail/i
not defined $r->{"fields_for"}{"EMAIL"}
Truth table0%0/3
A and not(B)
A= $field =~ /(e-?)?mail/iB= not defined $r->{"fields_for"}{"EMAIL"}
| |||||||||||||||||||||||||||||
| 72 | 0 | log_info "Will be using field '$field' for VCF field 'EMAIL'"; | |||||||||||||||||||||||||||
| 73 | 0 | $r->{fields_for}{EMAIL} = $field; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($field =~ /cell|hp|phone|wa|whatsapp|te?le?[fp](on)?/i && !defined($r->{fields_for}{CELL})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$field =~ /cell|hp|phone|wa|whatsapp|te?le?[fp](on)?/i and not defined $r->{"fields_for"}{"CELL"}
MC/DC0%0/2
$field =~ /cell|hp|phone|wa|whatsapp|te?le?[fp](on)?/i and not defined $r->{"fields_for"}{"CELL"}
$field =~ /cell|hp|phone|wa|whatsapp|te?le?[fp](on)?/i
not defined $r->{"fields_for"}{"CELL"}
Truth table0%0/3
A and not(B)
A= $field =~ /cell|hp|phone|wa|whatsapp|te?le?[fp](on)?/iB= not defined $r->{"fields_for"}{"CELL"}
| |||||||||||||||||||||||||||||
| 76 | 0 | log_info "Will be using field '$field' for VCF field 'CELL' (cellular phone)"; | |||||||||||||||||||||||||||
| 77 | 0 | $r->{fields_for}{CELL} = $field; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (!defined($r->{fields_for}{N})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | die [412, "Can't convert to VCF because we cannot determine which field to use as the VCF N (name) field"]; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (!defined($r->{fields_for}{EMAIL})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | log_warn "We cannot determine which field to use as the VCF EMAIL field"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (!defined($r->{fields_for}{CELL})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | log_warn "We cannot determine which field to use as the VCF CELL (cellular phone) field"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 92 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | $r->{vcard} .= join( | ||||||||||||||||||||||||||||
| 95 | "", | ||||||||||||||||||||||||||||
| 96 | "BEGIN:VCARD\n", | ||||||||||||||||||||||||||||
| 97 | "VERSION:3.0\n", | ||||||||||||||||||||||||||||
| 98 | "N:", $r->{input_row}[$r->{input_fields_idx}{ $r->{fields_for}{N} }], "\n", | ||||||||||||||||||||||||||||
| 99 | (defined $r->{fields_for}{EMAIL} ? ("EMAIL;type=INTERNET;type=WORK;pref:", $r->{input_row}[$r->{input_fields_idx}{ $r->{fields_for}{EMAIL} }], "\n") : ()), | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | (defined $r->{fields_for}{CELL} ? ("TEL;type=CELL:", $r->{input_row}[$r->{input_fields_idx}{ $r->{fields_for}{CELL} }], "\n") : ()), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 101 | "END:VCARD\n\n", | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | on_end => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 107 | 0 | $r->{result} = [200, "OK", $r->{vcard}]; | |||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | 1 | ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||||||||||
| 112 | # ABSTRACT: Create a VCF from selected fields of the CSV | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||