#!/usr/bin/perl -w
use warnings;
use diagnostics;
use Switch;
open FH, "<$ARGV[0]" or die "$!";
sub commandType{
print "comm entered for $_";
switch($_){
case("add") {print "this is add\n"}
case("sub") {print "this is sub\n"}
case("neg") {print "this is neg\n"}
case("eq") {print "this is eq\n"}
case("gt") {print "this is gt\n"}
case("lt") {print "this is lt\n"}
case("and") {print "this is and\n"}
case("or") {print "this is or\n"}
case("not") {print "this is not\n"}
}
}
while(<FH>){
next if /^\s*\/\//;
next if /^\s*$/;
my $line = "$_";
$line =~ s/\s+$//;
print "$line\n";
commandType($line);
}
Here is my code which takes the input from the following file supplied to it through the command line:
// Pushes and adds two constants.
push constant 7
push constant 8
add
For each line of the file above the perl code will run the subroutine commandType
to check if it is among the given cases inside the subroutine and prints if it is. But even though the add command is present in the file above the code still does not print it. I am getting the following output:
push constant 7
comm entered for push constant 7
push constant 8
comm entered for push constant 8
add
comm entered for add`
Why is case "add" not printing anything?