2012-05-01 16:25:27 -07:00
|
|
|
package DDG::Goodie::Palindrome;
|
2012-05-01 16:28:39 -07:00
|
|
|
# ABSTRACT: Return if the a string is a palindrome, formatted requests:
|
|
|
|
# 'is <string> a[n] palindrome[?]' or 'isPalindrome <string>'
|
2012-05-01 16:25:27 -07:00
|
|
|
|
|
|
|
use DDG::Goodie;
|
|
|
|
|
2012-05-02 12:05:09 -07:00
|
|
|
triggers any => 'palindrome';
|
2012-05-01 16:25:27 -07:00
|
|
|
|
2012-05-23 19:05:08 -07:00
|
|
|
zci is_cached => 1;
|
2012-05-01 16:25:27 -07:00
|
|
|
|
2012-05-23 19:05:08 -07:00
|
|
|
handle query => sub {
|
2012-05-01 16:25:27 -07:00
|
|
|
#Remove the trigger text from the query.
|
2012-05-09 06:52:32 -07:00
|
|
|
return unless /^(?:is\s+|)(.*?)\s+an?\s*palindrome\??$/i;
|
|
|
|
|
|
|
|
my $palindrome = $1;
|
|
|
|
|
|
|
|
my $is_palindrome = 0;
|
|
|
|
|
|
|
|
# Clean up.
|
|
|
|
my $clean_palindrome = lc $palindrome;
|
|
|
|
$clean_palindrome =~ s/[^a-z0-9]+//g;
|
|
|
|
|
|
|
|
$is_palindrome = 1 if $clean_palindrome eq scalar reverse $clean_palindrome;
|
2012-05-01 16:26:52 -07:00
|
|
|
|
|
|
|
#Check to see if it is a palindrome.
|
2012-05-09 06:52:32 -07:00
|
|
|
return $is_palindrome ? qq("$palindrome" is a palindrome.) : qq("$palindrome" is not a palindrome.);
|
2012-05-01 16:25:27 -07:00
|
|
|
};
|
|
|
|
|
2012-05-09 06:52:32 -07:00
|
|
|
1;
|