Hacking Gmail

(Grace) #1

180 Part III — Conquering Gmail


Showing Your Current Contacts


Once you’ve got your old contacts in there and have added a load more, you might
want to list those and access them programmatically. Listing 12-2 shows you how.

Listing 12-2: Getting Your Contacts

use Utils;

$gmail = login();

(@contacts) = @{ $gmail->get_contacts() }; # simply get all
contacts
foreach (@contacts) { # and iterate
though them
print $_->{“name1”}. “\t”. $_->{“email”}. “\n”; #
output contact data
}

The Mail::Webmail::Gmail module provides for this with one lovely bite-sized
function:get_contacts(). This returns an array hash of your contacts, in this
format:
$contact{ ‘id’ }
$contact{ ‘name1’ }
$contact{ ‘name2’ }
$contact{ ‘email’ }
$contact{ ‘note’ }

And so, in the core of the script in Listing 12-2, you are just looping through the
Arrays of Hashes and printing out the first name and e-mail address. You could,
of course, change this to use the other values, too:
foreach (@contacts) {
print $_->{“name1”}. $_->{“name2”}. $_->{“id”}. “\t”.
$_->{“email”}. “\t”. $_->{“note”}. “\n”;
}

The get_contacts()function can also be limited to the Frequently Mailed
contacts with the frequent flag:
my $contacts = $gmail->get_contacts( frequent => 1 );
Free download pdf