I have been encountering errors in Perl code that are reported at places which have nothing to do with the error. I fixed one such error after hours of removing and re adding code line by line and then doing some trial and error. Two such errors are detailed below. My question is : If these issues happen in future, is there a way to ensure that perl compiler helps me fix this OR do I have to rewrite the code in some other language. (am considering Java).
My program looks like this:
use switch;
use strict;
use warnings;
...other modules;
sub log{
}
..various sub routines
switch {$val1)
{
log(..) #first invocation of log
case ($val2)
...
}
Now within sub log if I do this
{
$val3 = POSIX::floor($val2/$val4)*$val4;
$val5="/x/y/$logfilename";
}
I get an error saying that there is an error at the case statement.
If I move the line $val5=”/x/y/$logfilename”; BEFORE $val3, there is no error.
OR if I remove the ‘/’ in $val5 i.e. $val5=”x”, there is no error
OR if I say $val5=qq(/x/y/$logfilename); there is no error.
This time I consider myself lucky that I found a workaround but it was only after 3 hours of fighting this. Is there a way to make perl compiler report the errors accurately?
I have one more similar case to report and can add if necessary.
Inputs solicited
Ignoring the problems with your post, the issue you are probably running into is with the fact that the
switchfunctionality in perl is implemented with a source filter. This basically means that the source code is preprocessed to convert what looks like a switch case structure into valid perl code before compilation. Perl’s parser usually gets line numbers a little wrong on complex structures, but when source filters are involved, all bets are off. This is one of the reasons thatSwitchhas been deprecated in modern versions of Perl.The solution is to stop using switch, and either use Perl’s other control structures, or use a version of perl new enough to support the
given/whenconstruct (5.10+), which is modern Perl’s version of switch/case.