ascetic has asked for the wisdom of the Perl Monks concerning the following question:
I am at a loss to diagnose a simple syntax error caused by a conspiracy between three perl (5.8.8 on RH-Linux) features.
1) a use constant pragma which performs a division using the / character
2) a comment line with single quote and the / character (changing to double quotes behaves the same way)
3) a switch-case construct
Here is the code example
#!/usr/local/bin/perl -w use strict ; use Switch ; use constant PI => 3.141592653589793 ; use constant HALFPI => ( PI / 2.0 ) ; # half use constant TWOPI => ( 2.0 * PI ) ; # double print TWOPI."\n"; print HALFPI."\n"; # directory is '/home''''''''' my $long = 4.567 ; # radians my $convention = 'W' ; switch ( $convention ) { case ('E') { print "east longitude $long r +adians\n"; } case ('W') { $long = TWOPI-$long ; print "west longitude $long r +adians\n"; } else { print "error: $convention not valid\n"; } };
% monks_help.pl syntax error at monks_help.pl line 11, near ") {" Execution of monks_help.pl aborted due to compilation errors.
Here are the variations that I have found by trial and error that will make the code run to completion:
a) changing the HALFPI line so that NO computations use the / character
use constant HALFPI => ( PI / 2.0 ) ; to use constant HALFPI => { 0.5 * PI } ;
b) changing the TWOPI line so that BOTH computations use the / character
use constant TWOPI => ( 2.0 * PI ) ; to use constant TWOPI => ( PI / 0.5 ) ;
c) removing the / character from the comment line
d) removing all the ' characters from the comment line
e) delete or add ' characters at the end of the comment line so that there are an even number (example has nine)
Obviously I can get this simple pared down example to work by making minor changes, but since use constant and Switch.pm are core parts of Perl 5.8.8 (along with comment capability of course), I would like to learn from what I may have overlooked, assuming for the moment that there is not a bug in the syntax checker.
Thanks in advance for what I am sure will be an interesting (and/or quick) discussion.BTW, the error does NOT occur on my Windows machine.
|
---|