Actual Perl question

Matt Plug matt.plug.org at gmail.com
Wed Mar 20 15:48:42 MDT 2019


On Wed, Mar 20, 2019 at 1:12 PM Charles Curley <
charlescurley at charlescurley.com> wrote:
>
> I would like to convert a spreadsheet to vcards. The closest I have
> come to anything useful is to use Perl's vCard::AddressBook module.
>
> If you know what your phone numbers are, you can do:
>
> # complex getters/setters
> $vcard->phones([
>     { type => ['work', 'text'], number => '651-290-1234', preferred => 1
},
>     { type => ['home'],         number => '651-290-1111' }
> ]);
>
> My spreadsheet has three columns for phones, home, work and mobile,
> which may or may not have a phone number for any given row. What I have
> now is:
>
>
>   if (length($fields[6]) > 0) {
>     $vcard->phones ([{type => ['home'], number => $fields[6]}]);
>   }
>   if (length($fields[7]) > 0) {
>     $vcard->phones ([{type => ['work'], number => $fields[7]}]);
>   }
>   if (length($fields[8]) > 0) {
>     $vcard->phones ([{type => ['mobile'], number => $fields[8]}]);
>   }
>
>
> If I call $vcard->phones twice, the second call obliterates the data
> passed in by the first call. So that doesn't work.
>
> I need to accumulate such phone numbers as I have, then pass them all
> in at once. My question is, how do I do that?

The phones() subroutine accepts an arrayref (of hashrefs) as its argument,
per the docs:https://metacpan.org/pod/vCard#phones()

You can simply push each phone number's hashref into your own array, then
pass it to phones() as an arrayref once it's complete for that vCard.  e.g.:

my @vcard_phones;

if (length($fields[6]) > 0) {
  push @vcard_phones, { type => ['home'], number => $fields[6] };
}
if (length($fields[7]) > 0) {
  push @vcard_phones, { type => ['work'], number => $fields[7] };
}
if (length($fields[8]) > 0) {
  push @vcard_phones, { type => ['mobile'], number => $fields[8] };
}

# note the "\" before @vcard_phones to pass it as a reference
$vcard->phones(\@vcard_phones);


More information about the PLUG mailing list