The problem is that you are using the assignment operator '=' rather than an equality operator ('==' or 'eq'). Assignment returns whatever was assigned and 'larry' (the first string assigned) is a true value so the first if test always succeeds.

If you find yourself writing the same code over and over again you should think about how you can rewrite it to avoid repeating yourself. In this case a look up table (hash for the initiated) is the answer. Consider:

use strict; use warnings; my %names = (larry => 'Larry', moe => 'Moe', curly => 'Curly'); print "What is your name?\n"; chomp(my $name = <STDIN>); if (exists $names{lc $name}) { print "You\'re $names{lc $name}!\n" } else { $name = ucfirst $name; print "I don't know anyone by the name $name\n"; }

Note that the hash keys are entered lower case but the hash values have the correct case and that the hash look up uses the lower case version of the given $name string. This lets names with characters of any case to match, but prints out the correct case.

The other thing to note is how easy it is to add another bunch of names. You could even read them in from a file with just a little effort.


True laziness is hard work

In reply to Re: elsif failing by GrandFather
in thread elsif failing by rcd^_-

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.