String 消除perl数字加法器代码中的警告

String 消除perl数字加法器代码中的警告,string,perl,input,warnings,String,Perl,Input,Warnings,我正在编写一个程序,从命令行中提取数字,直到用户输入一个空行 如果用户输入的内容既不是换行符也不是数字,它会通知用户并继续 虽然一切正常,但我已经启用了use warnings,而且似乎不喜欢第二个if-conditional,如果输入了无效的内容 Argument "foo" isn't numeric in numeric eq (==) at adder.pl line 25, <STDIN> line 4. 参数“foo”在adder.pl第25行第4行的数字等式(=)中不

我正在编写一个程序,从命令行中提取数字,直到用户输入一个空行

如果用户输入的内容既不是换行符也不是数字,它会通知用户并继续

虽然一切正常,但我已经启用了use warnings,而且似乎不喜欢第二个if-conditional,如果输入了无效的内容

Argument "foo" isn't numeric in numeric eq (==) at adder.pl line 25, <STDIN> line 4.
参数“foo”在adder.pl第25行第4行的数字等式(=)中不是数字。
我不喜欢用这个警告运行程序。如何改进代码

这是我的节目

#!/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";
#/usr/bin/perl
严格使用;
使用警告;
#声明变量
我的$number=0#最终答案
我的$input;
#提示用户
打印“逐行输入一些整数。完成后,按回车键将它们相加。”。“\n”;
而(1){
#从用户处获取输入
$input=;
#删除换行符
chomp($输入);
#换行符中的用户pnches
如果($input eq“”){#如果答案是新行
#退出循环
最后;
}#if语句结尾
#用户输入错误
elsif($input==0&&$input ne'0'&&$input ne'){
#告诉用户发生了什么以及如何纠正
打印“输入必须是整数”。“\n”;
}#elsif语句结束
否则{
chomp($输入);
$number+=$input;
}#else语句结尾
}#结束时
打印“总计为:$number\n”;

由于Perl是非类型化的,并且您使用$input作为数字和字符串,因此会收到该警告,因为“foo”不是数字,“==”用于比较数字的相等性

首先需要检查$input是否为数字。一项建议:

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

由于Perl是非类型化的,并且您使用$input作为数字和字符串,因此会收到该警告,因为“foo”不是数字,而“==”用于比较数字的相等性

首先需要检查$input是否为数字。一项建议:

if ($input =~ /^\d+$/)
{
  $number += $input;
}
else
{
  print "Input must be an integer.\n";
}
Perl做得很好。它以它而闻名

所以,不管您来自哪种语言—看起来像C—都不要检查字符串和数字:Perl标量变量是您要求的任何变量

这意味着

elsif ($input == 0 && $input ne '0' && $input ne '') {
没什么意义。从键盘读取的任何内容最初都是一个字符串,但如果需要,它将是一个数字。您要求
$input
计算为零,但不是文本字符串
0
。这适用于极少数字符串,例如
00
0e0

我想这就是你想写的。请看一看

没有评论不是更清楚吗

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";
使用严格;
使用警告;
打印“逐行输入一些整数。完成后,按回车键将它们相加\n”;
我的$total=0;
而(){
咀嚼;
最后除非/\S/;
如果(/\D/){
打印“输入必须是整数\n”;
下一个
}
$total+=$\ux;
}
打印“总计为:$Total\n”;
Perl做得很好。它以它而闻名

所以,不管您来自哪种语言—看起来像C—都不要检查字符串和数字:Perl标量变量是您要求的任何变量

这意味着

elsif ($input == 0 && $input ne '0' && $input ne '') {
没什么意义。从键盘读取的任何内容最初都是一个字符串,但如果需要,它将是一个数字。您要求
$input
计算为零,但不是文本字符串
0
。这适用于极少数字符串,例如
00
0e0

我想这就是你想写的。请看一看

没有评论不是更清楚吗

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";
使用严格;
使用警告;
打印“逐行输入一些整数。完成后,按回车键将它们相加\n”;
我的$total=0;
而(){
咀嚼;
最后除非/\S/;
如果(/\D/){
打印“输入必须是整数\n”;
下一个
}
$total+=$\ux;
}
打印“总计为:$Total\n”;

您的Perl代码的布局很难阅读,为了理解您的意思,我已经在perltidy中运行了它。注释仍然是一个问题,它们使您的代码难以阅读。如果您的程序需要注释,那么您的编码风格通常有问题。如果您必须添加注释,那么应该说明代码为什么要做某事,而不是它正在做什么:代码本身告诉您这一点。在这里,没有任何注释就更清楚了,但是我把您的注释放在一边,只修改了空格,这样其他人就可以看到您编写的所有内容。Perl代码的布局很难阅读,我已经通过
perltidy
运行了它,以便理解您。注释仍然是一个问题,它们使您的代码难以阅读。如果您的程序需要注释,那么您的编码风格通常有问题。如果您必须添加注释,那么应该说明代码为什么要做某事,而不是它正在做什么:代码本身告诉您这一点。在这里,没有任何注释就更清楚了,但是我没有理会你的注释,只修改了空格,这样其他人就可以看到你写的所有内容