As noted in the comments, you are using an outdated Switch module. You probably forgot to add use Switch , so the switch keyword is not imported. If you want this functionality, you should use the use feature qw(switch) , which instead uses the given , when and default keywords instead.
Your error message, which is rather vague, refers to perl not processing switch ($variable) { ... } as a valid statement. The compiler believes that it sees the function because of the bareword switch , followed by parens, but the next block { ... } causes an error.
This has nothing to do with the code that precedes it, and the code works for me if I add use Switch .
It is debatable if this particular attribute is used for this code is a good choice.
use feature qw(switch); sub getName { my $tempName = $_[0]; given ($tempName) { when ('Cart Software') { return 'CART' } when ('OX-Gift-Hyderabad') { return 'Gift' } default { return $_[0]} } }
Equivalent without relying on the switch:
sub getName { my $name = shift; return "CART" if $name eq "Cart Software"; return "Gift" if $name eq "OX-Gift-Hyderabad"; return $name; }
I think this is preferable, as it is more clear what type of comparison is made.
ETA: Your getAge routine can be written much more efficiently. I took the liberty of removing your random capitalizations because they are evil, and perl really distinguishes between aFunctionForGettingStuff and aFunctionForGettingStuff .
I see that you are using some kind of homemade way to cut the float. This is optional since perl has a built-in function that does this: int ()
sub getage { my $createtime = shift;
You donβt need to add statements into one, but I think itβs a good idea to remove as many transition variables as possible. However, I believe that it is necessary - in terms of readability and good practice - not to declare variables until you use them. That way, when you read the code and see my $foo = ... , you know that $foo declared and assigned then and there.
You can even remove the $age variable, but I feel like it adds something to readability to leave it behind.