You have problems in your logic to decide what to do. A single equal sign "=" is used for assignment, so when you write
if ( $answer = "n" )
what is really happening is you are assigning "n" to the variable answer. The value of the assignment is the thing being assigned, namely "n", which evaluates to true, so the if condition is satisfied.

What you need is string comparison, in this case "eq". "eq" evaluates to true is the strings are equal, false otherwise. So, what you probably want is:
if ( $answer eq "n" ) {
This still wont catch the case where the user enters "N" (capitalized). There are many ways to deal with this, i prefer:
if ( uc($answer) eq "N" ) {
This will uppercase the value of $answer, so then you only need to check it against the capital "N".

Your caveat was twofold:
1)using an assignment operator when you should be using a comparison operator.
2)using a numeric operator when a string operator is called for.

for #1, you just need to be careful.
for #2, the easiest way to remember what to use is: when doing numeric comparisons, use the normal math symbols (<, >, =, etc), and when doing string comparisons, use the comparators that are letters (eq, ne, lt, gt, etc)

One final note, you dont need to use the "&" to denote a subroutine call anymore. you can just say
confirm()
hope this helps.

In reply to Re: A missleading sub by shemp
in thread Trouble with flow control by eoin

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.