<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

=head1 NAME

ddg.pl -- Query the Duck Duck Go search engine.

=head1 SYNOPSIS

In the shell:

    $ ddg.pl duck duck go
    $ ddg.pl '!date longcat'

Or in your program:

    do 'ddg.pl';
    my $result = ddg($query);

=cut

use JSON::PP;
use HTTP::Tiny;

sub ddg
{
    my $q = shift;
    my $h = HTTP::Tiny-&gt;new;
    my $res = $h-&gt;get(
        'http://api.duckduckgo.com/?format=json&amp;q='
            . $h-&gt;www_form_urlencode({
                format =&gt; 'json',
                q =&gt; $q
            }));
    die unless $res-&gt;{success};
    $res = decode_json($res-&gt;{content});
    for (keys %$res) {
        delete $res-&gt;{$_} if $res-&gt;{$_} eq '';
    }
    $res;
}

if (!defined caller) {
    eval 'use Data::Dumper';
    print Dumper(ddg("@ARGV"));
}

__END__

=head1 DESCRIPTION

C&lt;ddg.pl&gt; provides basic access to the Duck Duck Go search engine API.
It supplies a single function, C&lt;ddg&gt;, which takes a query string and
returns the API result as a hash reference.  It has no non-core
dependencies on Perl 5.14 or newer, and requires only L&lt;HTTP::Tiny&gt;
and L&lt;JSON::PP&gt; on older Perls.

=head1 SEE ALSO

The Duck Duck Go API page, L&lt;http://duckduckgo.com/api.html&gt;.

L&lt;WWW::DuckDuckGo&gt;, a heavier and more full-featured module.

L&lt;App::DuckDuckGo&gt;, a heavier command-line program.

=head1 AUTHOR

Sean O'Rourke, E&lt;lt&gt;seano@cpan.orgE&lt;gt&gt;

Bug reports welcome, patches even more welcome.

=head1 COPYRIGHT

Copyright (C) 2012 Sean O'Rourke.  All rights reserved, some wrongs
reversed.  This module is distributed under the same terms as Perl
itself.

=cut

</pre></body></html>