This repository is for contributing goodies, which are special tools that reveal instant answers at the top of search results, e.g. calculations or throwing dice.
Most of the existing goodies are listed on the [goodies page](http://duckduckgo.com/goodies.html) and [tech goodies page](http://duckduckgo.com/tech.html).
The external variables used in the goodie get defined at the top of the script. See [dice](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/dice/goodie.pl) for a good example.
Secondly you want to segment the query space to queries related to that goodie. [guid](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/guid/goodie.pl) uses a hash to do so.
[binary](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/binary/goodie.pl) uses a regular expression.
```perl
if (!$type && $q_check_lc =~ m/^binary (.*)$/i) {
}
```
For regular expressions, we need to watch out for false positives and speed.
3) Once inside the conditional, the goodie formulates the answer. This could vary slightly depending on input, but results in setting the $answer_results variable. Here's what [abc](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/abc/abc.pl) looks like.
```perl
if (!$type && $q_check =~ m/^\!?\s*[A-Za-z]+(\s+or\s+[A-Za-z]+)+\s*$/ ) {
my @choices = split(/\s+or\s+/, $q_check);
my $choice = int(rand(@choices));
$answer_results = $choices[$choice];
$answer_results .= ' (random)';
$answer_type = 'rand';
}
```
And here are some other things to keep in mind:
4) If you need a helper file, name it goodie.txt or goodie.html as needed. If you need to read in that file to be used over and over again, do it outside the conditional. For example [passphrase](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/passphrase/goodie.pl) reads in a list at the top.
```perl
my %passphrase = ();
open(IN, '<goodie.txt');
while (my $line = <IN>) {
chomp($line);
my @res = split(/ /, $line);
$passphrase{$res[0]} = $res[1];
}
close(IN);
```
Whereas if you need to read in a file for output, do it inside the conditional. For example, [public_dns](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/public_dns/goodie.pl) reads in a list inside.
5) If it is possible that the conditional gets called, but $answer_results still may not be set, then wrap $answer_type (and possibly other variables) in a separate conditional like in [private_network](https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/private_network/goodie.pl).