kevin su kevin su - 2 years ago 59
Perl Question

How do I use an 'if' statement in 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";
while(<INFILE>) {
$totalbytes += $data[1];
print "Total number of bytes printed: $totalbytes\n";

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

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

But here when I run
/ -v
./ -s printing_usage_file
it prints
my name
and the
. How can I fix it?

Answer Source

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";'

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";'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download