Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I don't know if this is even possible, let alone how to do it, but what I'd like to develop is a perl script that would accept as input a 10 digit USA/Canada telephone number and match it to a record in Google Contacts, then return the name associated with that record.

One problem is that Google does not attempt to normalize numbers in their contacts database, therefore in a given contact the phone number might include spaces, hyphens, parenthesis, etc. Basically all non-numeric characters would need to be stripped before the comparison is made. The only exception is where ::: appears, that seems to separate two similar items of the same type in the same field, such as two different phone numbers of the same type. And in any one record there are several fields that can potentially contain phone numbers. Anyway what I really want to to is type in scriptname.pl 9999999999 (with the 9999999999 replaced by the actual ten digit number) and have it find the record containing that number in my Google Contacts, and print the associated name, and I have no idea where to start. Any help would be appreciated!
  • Comment on How can I get a single record from Google Contacts based on a phone number?

Replies are listed 'Best First'.
Re: How can I get a single record from Google Contacts based on a phone number?
by kennethk (Abbot) on Mar 27, 2015 at 00:37 UTC
    What have you tried? What worked? What didn't? What is your experience level with Perl? See How do I post a question effectively?.

    Interaction with Google contacts can be accomodated with WWW::Google::Contacts (or this if CPAN search is down).

    Checking the number is likely accomplished with mangling the input into a regular expression; perhaps like:

    my @terms = $ARGV[0] =~ /(\d{3})(\d{3})(\d{4})/; my $re = qr/^\(?$terms[0]\)?-?$terms[1]-?$terms[2]$/;
    or, following your suggested spec
    $number =~ s/[^\d:]//g; if (grep $_ eq $ARGV[0], split ':::', $number) { say $number; }
    If you are unfamiliar with regular expressions, see perlretut.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I haven't tried anything so far because I literally had no idea where to start. I've been reading the page on WWW::Google::Contacts and it looks like this may be the tool I need. My only mental hurdle will be figuring out how to use it effectively. Also, the REGEX is a huge help, I was never any good at those.

      As far as my experience level, in many respects I'm a rank beginner. I had some very brief exposure to Perl several years ago but I am old enough (don't ask, but I'm old enough to remember when a pe(a)rl was something dropped into a bottle of Prell shampoo in TV commercials) where if I haven't done something in the last week or two, the memory of how to do it fades quickly. I wrote a few very short programs back then to do small tasks and I am actually still using a couple of them, and this is another of those small tasks I was hoping Perl might be well suited for. So thanks for giving me a push in the right direction; it may take me a few days to figure it all out but at least now I have something to work with!

      As it turns out, WWW::Google::Contacts will not install. After installing MANY other modules (an unbelievable number), it failed because it could not install some dependencies (I forget which ones now). So, that's not going to work, I guess, and there is no way I would try installing it again because of the HUGE number of other modules it brought in. I had to revert my system back to a snapshot I had made earlier just to get rid of the mess.

      Perl never used to be like that; I remember when installing modules that sometimes it would bring in two or three others but that thing literally installed modules for over an hour, asking me EVERY SINGLE TIME it found yet another dependency it thought it needed to install. Absolutely ridiculous! Thanks for your help anyway, I'm not faulting you for suggesting it, in fact I'm not sure who's at fault but I just lost any desire I had to mess with Perl again.

        there is no way I would try installing it again because of the HUGE number of other modules it brought in.

        If this (the total number of dependencies) is important to you, then you might find it useful in future cases to look at deps.cpantesters.org where you can see the full dependency tree of any module. You can then judge whether the installation would be worth the effort.

        asking me EVERY SINGLE TIME it found yet another dependency it thought it needed to install. Absolutely ridiculous!

        Of course whichever utility you have used to perform the installation will have an option to automatically install all the dependencies without asking this question every time. Since you have not said which utility you used, I cannot advise you further. If you consult the documentation (even the usage message) for that utility it should be clear enough which option to use.

        It is helpful to say which one would not install and take a look at why. Sometimes it is a matter of getting a non-Perl rerequisite (such as the GCC). But usually this can be addressed or we can help you work on getting it fixed.