Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用';如果';Perl中的语句?_Perl - Fatal编程技术网

如何使用';如果';Perl中的语句?

如何使用';如果';Perl中的语句?,perl,Perl,考虑: #!/usr/bin/perl if ($ARGV[0]=="-v") { print "MY name: abc\n"; print "MY student ID:111110\n"; } if ($ARGV[0]=="-s" && $ARGV[1]==printing_usage_file) { open (INFILE, $ARGV[1]) or die "An input file is required as argument\n";

考虑:

#!/usr/bin/perl
if ($ARGV[0]=="-v") {
    print "MY name: abc\n";
    print "MY student ID:111110\n";
}
if ($ARGV[0]=="-s" && $ARGV[1]==printing_usage_file) {
    open (INFILE, $ARGV[1]) or die "An input file is required as argument\n";
    $totalbytes=0;
    $data="";
    while(<INFILE>) {
        chomp();
        @data=split(/,/);
        $totalbytes += $data[1];
    }
    print "Total number of bytes printed: $totalbytes\n";
}
#/usr/bin/perl
如果($ARGV[0]==“-v”){
打印“我的名字:abc\n”;
打印“我的学生ID:11111 0\n”;
}
如果($ARGV[0]==“-s”&&&$ARGV[1]==打印使用情况文件){
打开(infle,$ARGV[1])或关闭“需要输入文件作为参数\n”;
$totalbytes=0;
$data=“”;
while(){
chomp();
@数据=分割(/,/);
$totalbytes+=$data[1];
}
打印“打印的总字节数:$totalbytes\n”;
}
在本例中,我想要的是当我运行
/perl.pl-v
时,它会打印
我的名字
id

当我运行
/perl.pl-s printing_usage_file
时,它会打印
总字节数(printing_usage_file file是如下列表:aaa、240、ccc)


但是在这里,当我运行
/perl.pl-v
/perl.pl-s printing\u usage\u file
时,它会打印
我的名字
id
总字节数
。如何修复它?

您的问题是,当需要使用
eq
运算符时,您正在使用
=
运算符比较两个字符串

$ perl -e 'print q{<}, "a" == "b", q{>}, "\n";'
<1>

如果我在第二个if语句中使用elsif,/perl.pl-s printing_usage_文件将打印我的姓名和ID,而不显示totalbytesAdd
use警告
使用严格
你会收到关于一个简单的单词“打印使用情况文件”的投诉。光说空话是不好的做法。您还将被告知使用
my
声明所有变量。请注意,标量
$data
在脚本的其余部分未使用。有经验的Perl程序员使用严格的警告来确保他们没有犯任何愚蠢的错误;出于同样的原因,新手Perl程序员也应该使用它们。
$ perl -e 'print q{<}, "a" eq "b", q{>}, "\n";'
<>