#!/usr/bin/perl -w

# Determining whether a given string is a palindrome.
# (Recursive approach)

sub isPalindrome($);

print "Enter a string > ";
chomp(my $str = <STDIN>);
if ($str ne '') {
	print "$str is ", isPalindrome($str)?"":"not ", "a palindrome.\n";
} else {
	print "The string should not be empty!\n";
}	

sub isPalindrome($) {
	my $string = $_[0];

	# A standalone character or an empty string are by definition symmetric.
	# This signifies the deepest recursion stack possible.
	if (length($string) <= 1) {
		return 1;
	}
	# Here, what we need to do is to examine the first
	# and last character, and invoke a new isPalindrome
	# to deduce whether the string in the middle is a palindrome.
	my $leftchar = lc substr($string, 0, 1, "");
	my $rightchar = lc substr($string, -1, 1, "");
	return $leftchar eq $rightchar && isPalindrome($string);
}
