如何使用';如果';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";'
<>