user1768884
user1768884

Reputation: 1047

Get rid of warning in perl number adder code

I am writing a program that takes numbers from the command line until the user enters a blank line.

Should the user enter something that is neither newline nor numeric, it notifies the user, and continues.

While everything works, I have use warnings turned on, and it doesn't seem to like the second if conditional if the enters something invalid.

Argument "foo" isn't numeric in numeric eq (==) at adder.pl line 25, <STDIN> line 4.

I don't like running the program with this warning. How can I improve my code?

This is my program

#!/usr/bin/perl
use strict;
use warnings;

#declare variable
my $number = 0;    #final answer
my $input;

#prompt the user
print "Input some integers, line by line. When you are done, press return to add them up." . "\n";

while (1) {

  #get input from user
  $input = <STDIN>;

  #remove newlines
  chomp($input);

  #user pnches in newline
  if ($input eq '') {    #if the answer is new line

    #quit the loop
    last;
  }    #end of if statement

  #user punches in bad input
  elsif ($input == 0 && $input ne '0' && $input ne '') {

    #tell the user what happened and how to rectify it
    print "Input must be an integer." . "\n";
  }    # end of elsif statement

  else {
    chomp($input);
    $number += $input;
  }    # end of else statement

}    #end of while

print "Total is: $number\n";

Upvotes: 1

Views: 95

Answers (2)

Borodin
Borodin

Reputation: 126722

Perl does DWIM very well. It is famous for it.

So, whatever language you have come from - it looks like C - forget about checking for both strings and numbers: a Perl scalar variable is whatever you ask it to be.

That means something like

elsif ($input == 0 && $input ne '0' && $input ne '') {

makes little sense. Anything read from the keyboard is initially a string, but it will be a number if you want. You are asking for $input to evaluate as zero but not to be the literal string 0. That applies to very few strings, for instance 00 or 0e0.

I think this is what you meant to write. Please take a look.

Isn't it clearer without comments?

use strict;
use warnings;

print "Input some integers line by line. When you are done, press return to add them up\n";

my $total = 0;

while (<>) {
  chomp;
  last unless /\S/;
  if (/\D/) {
    print "Input must be an integer\n";
    next;
  }
  $total += $_;
}

print "Total is: $total\n";

Upvotes: 3

Moffat
Moffat

Reputation: 39

Since Perl is untyped, and you are using $input as both a number and a string, you get that warning because "foo" isn't a number and "==" is used to compare equality of numbers.

You first need to check to see if $input is a number or not. One suggestion:

if ($input =~ /^\d+$/)
{
  $number += $input;
}
else
{
  print "Input must be an integer.\n";
}

Upvotes: 1

Related Questions