Some additional observations:

  1. No need to declare $current_number before the loop. It’s a lexical variable which is not used outside of the loop, so declare it in the while condition:

    while (my $current_number = <STDIN>)
  2. No point in testing whether $current_number is defined: anything returned by <STDIN> will contain a newline (that’s why you chomp it!), so must be defined.

  3. You say the script works, but there is no elegant way for the user to break out of the loop. Give the user an option, for example “Q” for quit, and test for it after the chomp:

    last if $current_number eq 'Q';
  4. You should add print statements to prompt the user for input, otherwise how are they supposed to know what to do?

    print "Enter a number ('Q' to quit):\n";

    You will need to add this print statement twice, once immediately before the loop and once at the end.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: What is wrong in this code? by Athanasius
in thread What is wrong in this code? by Anonymous Monk

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.