Error using (/) splitting operator in perl cgi?

I use the following function to calculate the number of days. The argument to setAge is the epoc time.

sub getAge { my $diff; my $age=0; my $sec=86400; my $createTime; my $currTime; $createTime = $_[0]; $currTime = UnixDate("now", "%s"); $diff = ($currTime - $createTime); $age =(($diff-($diff%$sec))/$sec); return $age; } 

But whenever I use the division operator, I get the following error

  syntax error at /apollo/env/ShiftReport/server-root/gcShiftReport.cgi line 616, near ")
     {"
 syntax error at /apollo/env/ShiftReport/server-root/gcShiftReport.cgi line 618, near "case 'OX-Gift-Hyderabad'"
 Execution of /apollo/env/ShiftReport/server-root/gcShiftReport.cgi aborted due to compilation errors. 

The error string comes from the following function definition.

 sub getName { my $tempName = $_[0]; switch ($tempName) { case 'Cart Software' { return 'CART' } case 'OX-Gift-Hyderabad' { return 'Gift' } else { return $_[0]} } } 

Can someone give some pointer on why this happens, and only when I use the division operator (/).

+4
source share
3 answers

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; # shift first argument off @_ my $sec = 86400; # use int() instead of removing remainder my $age = int((UnixDate("now", "%s") - $createtime) / $sec); return $age; } 

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.

+3
source

It seems to me that you are missing

 use Switch; 

from the beginning of your program. But this is an old version of the switch . If you are using Perl version 5.10 or later, you can replace this with

 use feature 'switch'; 

described here . The syntax uses given / when / default instead of switch / case / else , and when conditions need parentheses around them, like the if condition. Your code should become

 sub getName { my $tempName = $_[0]; given ($tempName) { when ('Cart Software') { return 'CART' } when ('OX-Gift-Hyderabad') { return 'Gift' } default { return $_[0]} } } 
+2
source

The Switch module is implemented as a source filter, it reads your source code and tries to convert the Switch statements to valid Perl code. I assume that you somehow managed to convince Switch that / starts the regex, hiding your Switch in what it considers a pattern. In this case, your Switch trivially rewritten as a hash search, which is likely to be more secure and faster.

 my %name_map = ( 'Cart Software' => 'CART', 'OX-Gift-Hyderabad' => 'Gift', ); sub getName { my $temp_name = $_[0]; if (exists $name_map{$temp_name}) { return $name_map{$temp_name}; } return $temp_name; } 
0
source

Source: https://habr.com/ru/post/1400869/


All Articles