rockmountain has asked for the wisdom of the Perl Monks concerning the following question:

The Perfectly running program
#!/usr/bin/perl # Palindrome.pl $x=<STDIN>; chomp($x); $y=reverse($x); if($x eq $y) { print "Palindrome"; }
Second program which doesn't execute
#!/usr/bin/perl # Palindrome.pl if((chomp($x=<STDIN>)) eq ($y=reverse($x))) { print "Palindrome"; }#not showing appropriate result. also I am not able watch the express +ion $x and $y in debug mode.
cheers Rock

Replies are listed 'Best First'.
Re: why the 2nd program doesn't work?
by matija (Priest) on Apr 20, 2004 at 12:16 UTC
    Because chomp $x returns the number of characters removed from $x - and that is rarely a palindrom of the string contained in $x...
      So what do you say. There is a error in syntax. I mean I am clueless about it. My C function funda says, it should work. Thanx for reply.
      cheers Rock
        No, it is an error in thinking.

        You are comparing

        chomp($x=<STDIN>)
        with
        $y=reverse($x)
        The value of chomp is most probably "1" or "2". The value of $y is the reverse of $x

        Your "C function funda" most certainly doesn't say that the return of $chomp function must be equal to it's argument.

        Hint:You can get information on perlfunctions by reading perldoc perlfunc, or if you're looking for a specific function, by doing perldoc -f functionname

Re: why the 2nd program doesn't work?
by perlinux (Deacon) on Apr 20, 2004 at 12:32 UTC
    Try:
    ... print chomp($x=<STDIN>); ...
    it returns 1 !

    From "Programming Perl":
    Unlike chop, chomp returns the number of characters deleted.