Howto create vCards with perl

I wanted to create vcards out of userdata from an application. There is a module called Text::vCard and the first impression was that it would be an easy job. Finally it took much more time than I thought because the documentation is not easy to understand and does not describe the API completly.

For this reason i want to write a small howto for the next time :-)

I wanted to get the textual output from the vcard. So I started creating an new Text::vCard, but could not find a method to output the content. After some reading I found out that the only way is to use am addressbook and create a vcard from this one:


my $addressbook = new Text::vCard::Addressbook();
my $vcard = $addressbook->add_vcard();

Next Step is to fill the base-data. Vcard knows base-data and “more complex” data. Note that $self is my user-object which contains the data.


# Basisdaten einfuegen
$vcard->fullname($self->get_fullname());
$vcard->email( $self->get('email') );
$vcard->uid($self->get('user'));
$vcard->title($self->get('title'));

Next Step is an Name-Node. This node belongs to the more complex ones and you can use. I do not know for what more than one of them can be used, but there must be any reason. The Method add_node is used to create a new node of a special type.


# namen setzen
my $name = $vcard->add_node({'node_type'=>'N'});
$name->family( $self->get('name') );
$name->given( $self->get('prename') );

Next i added an address-node. You can add multiple here i think. I did not try, but in other nodes you can do something like that: $add->add_types(’WORK’);


# Adresse einsetzen
my $ad = $vcard->add_node({'node_type'=>'ADR'});
$ad->add_types('work');
$ad->street( $self->get('street') );
$ad->city( $self->get('city') );
$ad->post_code($self->get('zip') );

Finally I added phone numbers. Please note the call add_types(['WORK', 'fax']) to mark that the phone-number is a fax at work.


# Telefonnummern
my $p = $vcard->add_node({'node_type'=>'TEL'});
$p->add_types('WORK');
$p->value($self->get('phone'));

my $fax = $vcard->add_node({'node_type'=>'TEL'});
$fax->add_types(['WORK', 'fax']);
$fax->value($self->get('fax'));

my $mobil = $vcard->add_node({'node_type'=>'TEL'});
$mobil->add_types('cell');
$mobil->value($self->get('mobil'));

Finally you can get the output using the addressbook-object:

my $textonly = $addressbook->export()

If you want to print this to a browser, do not forget to send a proper content-type like text/v-card, so the browser knows what to to with it.

  • Both comments and trackbacks are currently closed.
  • Trackback URI: http://www.frankl.info/wordpress/uncategorized/howto-create-vcards-with-perl/trackback
  • Comments RSS 2.0

Comments are closed.