0

Consider:

#!/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";
}

In this case, what I want is when I run ./perl.pl -v, it prints my name and id.

When I run ./perl.pl -s printing_usage_file, it prints the totalbytes(printing_usage_file file is list like:aaa,240,ccc).

But here when I run /perl.pl -v or ./perl.pl -s printing_usage_file it prints my name,id and the totalbytes. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kevin su
  • 67
  • 7
  • if i use elsif in the second if statement, /perl.pl -s printing_usage_file will print my name and ID without the totalbytes – kevin su May 29 '15 at 04:33
  • 2
    Add `use warnings;` and `use strict;` and you will get a complaint about a bare-word 'printing_usage_file'. It's not good practice to use bare words. You'd also get told to declare all your variables with `my`. Note that the scalar `$data` is unused in the rest of the script. Experienced Perl programmers use strict and warnings to make sure they haven't made any silly mistakes; novice Perl programmers should use them too, for the same reason. – Jonathan Leffler May 29 '15 at 05:14

1 Answers1

2

Your issue is that you are using the == operator to compare two strings when you need to use the eq operator.

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

If you use the eq operator, it will return an empty string which is a false value.

$ perl -e 'print q{<}, "a" eq "b", q{>}, "\n";'
<>
b4hand
  • 9,550
  • 4
  • 44
  • 49