90 Part I — Basics
Listing 5-27 (continued)
print “Connected to database\n”;
}
else
{
print “Couldn’t connect\n”;
}
my $sth = $dbh->prepare(“select * from restaurants”);
$sth->execute();
while(my $row = $sth->fetchrow_hashref())
{
print Dumper($row),”\n”;
}
$dbh->disconnect();
In Listing 5-27, a hash reference containing the row data is returned for each row in the table.
The hash in each case contains the field names (in the keys) and the corresponding value (in
the value). The Data::Dumpermodule is used to output a dump of the hash; the output
generated after inserting a single row is shown in Listing 5-28.
Listing 5-28: Dumped Data from the Database with Perl
Connected to database
$VAR1 = {
‘lat’ => ‘52.9111’,
‘name’ => ‘China Inn’,
‘id’ => ‘1’,
‘lng’ => ‘-0.639167’
};
Because the Google Maps API requires the information to be in XML format, Listing 5-29
shows an example of the same script returning an XML representation of each restaurant.
Listing 5-30 contains the generated XML for reference.