Here's how I'd do it...

use Modern::Perl; say "Enter a word... maybe it's a palindrome...?"; chop(my $input = lc <>); my $reversed = join q(), reverse split q(), $input; say $reversed eq $input ? "yeah" : "nah";

... but don't submit that as-is. Learn from it and write your own.

The chop() function I only use once here, to remove the line-break character from the user input. chop not only tells you what the last character was in the string, it also modifies the string! Thus your comparison at the end will never work, because by the time you've chopped $user nine times, it'll be empty.

(Plus comparing an array and a string using "==" doesn't do what you seem to think it will do.)

Here's a version somewhat closer in spirit to your version:

use Modern::Perl; # Get a line from the user, and remove the # trailing line break say "Enter a word..."; chop(my $user = lc <>); # Keep a copy of the user input because we're # about to totally destroy $user. my $original = $user; # An array that we're going to split the # input into. my @userinput; # Keep going while there's still some input. while (length $user) { # Chop takes a character from the end # of the string. Unshift adds something # to the start of the array. So this # reverses the string as it goes! unshift @userinput, chop $user; } # Now join the array back into a string. my $reversed = join q(), @userinput; if ($original eq $reversed) { say "PALINDROME"; } else { say "NOT A PALINDROME"; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Please help by tobyink
in thread Please help by Camerontainium

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.