#!/usr/bin/perl -w

use strict;

use DBI;
use Text::TabularDisplay;

my $query = "@ARGV" || "perl";

# Set up SQL statement -- note the multiple lines
my $sql = qq~
    SELECT
      title, URL, hostName
    FROM
      google
    WHERE
      q = "$query"
  ~;

# DBI/DBD options:
my %opts = (
    RaiseError => 1,         # Standard DBI options
    PrintError => 0,
    lr         => ['en'],    # DBD::Google options
    oe         => "utf-8",
    ie         => "utf-8",
);

# Get API key
my $keyfile = glob "~/.googlekey";

# Get database handle
my $dbh = DBI->connect( "dbi:Google:", $keyfile, undef, \%opts );

# Create Text::TabularDisplay instance, and set the columns
my $table = Text::TabularDisplay->new;
$table->columns( "Title", "URL", "Hostname" );

# Do the query
my $sth = $dbh->prepare($sql);
$sth->execute;
while ( my @row = $sth->fetchrow_array ) {
    $table->add(@row);
}
$sth->finish;

print $table->render;

