Q:

Case statement in perl: not able to enter proper case

#!/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? 

A:

EXPLANATION
The problem is that $_ doesn't automatically refer to the first argument passed to a sub, currently you are reading the same $_ as the one in your while-loop.
The value of $_ when inside commandType is the line read, still having the potential new-line attached to it, and since "add\n" isn't equal to "add", your case isn't entered.

SOLUTION
It would be preferred to change the contents of sub commandType to the below:
sub commandType{
  my $cmd = shift; # retrieve first argument
  print "comm entered for $cmd";
  switch($cmd) {
    ...
  }
}

