[EMAIL PROTECTED] wrote ..

>In http://www.cpan.org/doc/FMTEYEWTK/is_numeric.html, 
>( Is it a number? ), Tom Christiansen writes:
>---------------------------------------------------------------
>If you do care about getting 0's, then do this: 
>  do { 
>       print "Number, please: ";
>       $answer = <STDIN>;
>       if ($answer == 0 && $answer ne '0') {
>         print "Bad number\n";
>       }
>     } until $answer;
>---------------------------------------------------------------
>
>I tried this with ActiveState perl version 5.005_03. I entered 0 and
>got a bad number. After thinking awhile, I chomped the $answer. This
>works for 0, ( i.e. No "Bad number message" and the loop repeats ). I
>think we need a chomp there ( so instead of '0\n' ne '0', it is '0' ne
>'0' ). Is it so or am I missing something?

you haven't missed anything .. the code should definitely have a chomp in
there

I notice that the copyright notice is 1996 .. so perhaps my memory is
failing me and that there was a version of Perl where <STDIN> did NOT grab
the line-ending character as well .. otherwise Tom really is human and has
made a mistake :)

basically any of the samples where a string comparison is done should be
chomping STDIN before the comparison because while the numeric comparison
will ditch the trailing garbage (which in this case is LF) the string
comparison obviously will not

-- 
  jason king

  In Spearfish, South Dakota, if three or more Indians are walking down
  the street together, they can be considered a war party and fired
  upon. - http://dumblaws.com/

Reply via email to