File Coverage

blib/lib/Text/SimpleVaddrbook.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Text::SimpleVaddrbook;
2              
3 2     2   63563 use Text::SimpleVcard;
  0            
  0            
4              
5             use warnings;
6             use strict;
7              
8             =head1 NAME
9              
10             Text::SimpleVaddrbook - a package to manage multiple vCard-files
11              
12             =head1 VERSION
13              
14             Version 0.01
15              
16             =cut
17              
18             our $VERSION = '0.01';
19              
20             =head1 SYNOPSIS
21              
22             This package provides an API to reading multiple vCard-files. A vCard is an
23             electronic business card. This package has been developed based on rfc2426.
24              
25             You will find that many applications (KDE Address book, Apple Address book,
26             MS Outlook, Evolution etc) use vCards and can export/import them.
27              
28             use Text::simpleAdrbook;
29              
30             my $aBook = SimpleAddrbook->new( '/path/to/addressbook/address.vcf');
31              
32             while( my $vCard = $aBook->next()) {
33             print "Got card for " . $vCard->getFullName() . "\n";
34             }
35              
36             =head1 FUNCTIONS
37              
38             =head2 new()
39              
40             my $aBook = Text::simpleAdrbook->new( 'foo.vCard', 'std.vcf');
41              
42             The method will create an addressbook-object and check for the existence
43             and accessibility of the provided vCards. It will produce a warning, if it
44             can not read a file.
45              
46             =cut
47              
48             sub new() {
49             my $class = shift;
50             my $self = {};
51              
52             $self->{ ndx} = -1;
53             foreach( @_) {
54             if( ! -r $_) {
55             warn "unable to access vCard-file '$_'";
56             next;
57             }
58             push( @{ $self->{ fn}}, $_);
59             }
60             bless( $self, $class);
61             }
62              
63             =head2 next()
64              
65             my $vCard = $aBook->next();
66              
67             This method will read the next C-entry in the list of C-files.
68             It returns the entry in a C-object (see also C). it will
69             return C when called after the last entry was returned.
70              
71             =cut
72              
73             sub next() {
74             my( $class) = @_;
75             my( $dat, $lin);
76              
77             while( 1) {
78             # open a new file if necessary
79             if( $class->{ ndx} < 0 or eof( $class->{ fh})) {
80             my @ary = @{ $class->{ fn}};
81              
82             close( $class->{ fh}) if( $class->{ ndx} >= 0);
83             return undef if( $class->{ ndx} >= $#ary);
84             $class->{ ndx}++;
85              
86             open( $class->{ fh}, "< $ary[ $class->{ ndx}]");
87             }
88             my $fh = $class->{ fh};
89              
90             #skip forward until next begin of vcard is found
91             my $vCardCnt = 0;
92             while( $lin = <$fh>) {
93             if( $lin =~ /^BEGIN:VCARD/) {
94             $vCardCnt++;
95             last;
96             }
97             }
98             next if( eof( $fh)); # eof reached -> go to next file
99              
100             $dat = $lin; # put start of vcard into buffer
101             while( $lin = <$fh>) {
102             $dat .= $lin;
103             $vCardCnt++ if( $lin =~ /^BEGIN:VCARD/);
104             $vCardCnt-- if( $lin =~ /^END:VCARD/);
105             return Text::SimpleVcard->new( $dat) if( $vCardCnt == 0);
106             }
107             }
108             }
109              
110             =head2 rewind()
111              
112             $aBook->rewind();
113              
114             This method will rewind the filepointers, so that the next call of the method C will
115             return the first entry of the first C provided. This method is useful to re-read the
116             addressbooks e.g. when they changed
117              
118             =cut
119              
120             sub rewind() {
121             my( $class) = @_;
122              
123             close( $class->{ fh}) if( $class->{ ndx} >= 0);
124             $class->{ ndx} = -1;
125             }
126              
127             =head1 AUTHOR
128              
129             Michael Tomuschat, C<< >>
130              
131             =head1 BUGS
132              
133             Please report any bugs or feature requests to C, or through
134             the web interface at L. I will be
135             notified, and then you'll automatically be notified of progress on your bug as I make changes.
136              
137             =head1 SUPPORT
138              
139             You can find documentation for this module with the perldoc command.
140              
141             perldoc Text::SimpleVaddrbook
142              
143              
144             You can also look for information at:
145              
146             =over 4
147              
148             =item * RT: CPAN's request tracker
149              
150             L
151              
152             =item * AnnoCPAN: Annotated CPAN documentation
153              
154             L
155              
156             =item * CPAN Ratings
157              
158             L
159              
160             =item * Search CPAN
161              
162             L
163              
164             =back
165              
166             =head1 COPYRIGHT & LICENSE
167              
168             Copyright 2008 Michael Tomuschat, all rights reserved.
169              
170             This program is free software; you can redistribute it and/or modify it
171             under the same terms as Perl itself.
172              
173             =cut
174              
175             1; # End of Text::SimpleVaddrbook