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

Can someone give some script suggestions.
#!/usr/bin/perl -w use strict; use DBI; use Google::Directions::Client; my $hostname = "localhost"; my $db = "sugarcrm"; my $dsn = "dbi:mysqlPP:database=$db;host=$hostname"; my $dbh = DBI->connect($dsn, "root", "skynet") or die 'CANT CONNECT!!! +'; my $drh = DBI->install_driver("mysqlPP"); #CREATE AND EXECUTE QUERY my $sth = $dbh->prepare("SELECT p.id, p.primary_address_street addres +s, p.primary_address_city city, p.primary_address_state state, p.prim +ary_address_postalcode zip FROM prospects p LEFT JOIN prospects_cstm pc ON p.id = pc.id_c WHERE deleted <> 1 AND pc.addr_status_c is null limit 0, 1000;"); $sth->execute; my ($id,$address1,$city,$prov,$postalcode,$dealer_id); my $count = 0; #BIND TABLE COLUMNS TO VARIABLES $sth->bind_columns(\$id,\$address1,\$city,\$prov,\$postalcode); while($sth->fetch()) { my $goog = Google::Directions::Client->new(); my $response = $goog->directions( destination => $address1 . ',' . $city . ',' . $prov +. ',' . $postalcode, origin => '467 W. Shaw Fresno, CA 93724', ); if ($response->status eq 'OVER_QUERY_LIMIT') { die "Error: OVER_QUERY_LIMIT"; } unless ($response->status eq 'OK') { my $upd = $dbh->prepare("UPDATE prospects_cstm SET addr_status='NO +T_FOUND'"); $upd->execute; print "Count: " . $count++ . "\t" . "ID: " . $id . "\t" . "Status +: " . $response->status . "\n"; next; } my $distance = sprintf "%.2f", $response->routes->[0]->legs->[0]- +>distance / 1000; my $duration = sprintf "%.2f", $response->routes->[0]->legs->[0]- +>duration / 60; my $upd = $dbh->prepare("UPDATE prospects_cstm SET distance_c='" . + $distance . "', duration_c=" . $duration . ", addr_status_c='" . $re +sponse->status . "', lat_c=" . $response->routes->[0]->legs->[0]->end_loc +ation->lat . ", lng_c=" . $response->routes->[0]->legs->[0]->end_loca +tion->lng . " WHERE id_c='" . $id . "'"); $upd->execute; print "Count: " . $count++ . "\t" . "ADDRESS: " . $address1 . "\t" + . "CITY: " . $city . "\t" . "STATE :" . $prov . "\t" . "Duration: " + . $duration . "\t" . "Distance: " . $distance . "\t" . "Status: " . +$response->status . "\n"; } $sth->finish;
Thank you in advance.

Replies are listed 'Best First'.
Re: Google MapsAPI
by aaron_baugher (Curate) on May 27, 2012 at 15:25 UTC

    Suggestions for what? To make it work? To make it faster? More efficient? More secure? Shorter? Cleaner, easier to read?

    For starters, I'll throw in that Perl can interpolate variables within double-quoted strings, so you don't have to concatenate long series of strings and variables like you're writing Javascript. Also, when you're joining a bunch of strings together with a common delimiter, the join function comes in handy. So your last print line could be much clearer:

    print join("\t", "Count: $count", "ADDRESS: $address1", "CITY: $city", "STATE: $prov", "Duration: $duration", "Distance: $distance", "Status: ".$response->status ), "\n"; $count++;

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Thanks for the tidbit and feedback. Should of specified to make efficient and cleaner...I know it works.

        Ok then, I don't think there's a whole lot you can do to improve a script this simple. There are some other places where you can put long concatenations into single double-quoted strings like I showed before, which (in my opinion, anyway) is clearer. You might also add some error checking to your DBI executes, dying/warning with $DBI::errstr if something goes wrong.

        On the subject of efficiency: in my experience, when you're fetching data from sites across the net, your script will almost always spend most of its time waiting for that data. Unless your database server is particularly overworked, your database lookup and updates won't take much time, so while there may be things you could do to speed them up (like indexes), it probably wouldn't affect the overall speed of your script significantly. If the API fetches can be sped up, that would have to be done within the module, not your script. You could speed things by forking/threading to do multiple fetches at once, but that might violate the API's rules.

        So for the most part, I'd say your script is fine. I'd make some style changes, but those are fairly subjective, and I doubt much can be done regarding efficiency.

        Aaron B.
        Available for small or large Perl jobs; see my home node.