zeroclickinfo-goodies/lib/DDG/Goodie/Palindrome.pm

30 lines
762 B
Perl
Raw Normal View History

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>'
use DDG::Goodie;
triggers any => 'palindrome';
zci is_cached => 1;
handle query => sub {
#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-09 06:52:32 -07:00
1;