Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Syntax error with use constant and switch with comment between

by ascetic (Novice)
on Dec 04, 2009 at 22:36 UTC ( [id://811172]=perlquestion: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re: Syntax error with use constant and switch with comment between
by ikegami (Patriarch) on Dec 04, 2009 at 23:05 UTC

    Don't use Switch. It creates problems, and they're impossible to debug.

    If you have 5.10, use given/when. See perlsyn

      I guess I assumed that a core module would not create problems; especially in such a trivial example. I decided to give Switch a try after years of looking the other way because it seemed experimental. No, I don't have 5.10 at work, but this may cause me to push for an update. Curious why you didn't suggest getting rid of use const or not using comments because they create problems? ;)

        The problem with the Switch module is that it use a source filter to implement the syntax, but the parser is not very robust and that means it is easily confused. Simply best not to use it. Bewildering that it was ever made a part of the core.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Curious why you didn't suggest getting rid of use const or not using comments because they create problems?

        Why would I suggest that you stop writing Perl so you can use an unnecessary and broken module?

Re: Syntax error with use constant and switch with comment between
by ikegami (Patriarch) on Dec 07, 2009 at 01:38 UTC

    Your particular problem occurs with 2.09 but not with 2.14. It was probably 2.13 that fixed it. My earlier advice to avoid Switch remains, though. It can create errors in legit code with no indication that it is the cause, so using it can cost you a lot of time.

      Thanks for looking into the version differences. I will take your advice and avoid Switch. I am proposing (to nonbelievers I work with) to use perl as a replacement for what is currently a custom sub-configuration file with logical selections based on settings in a top-level config file. The switch statement would be more familiar and therefore more likely to be favorably received (than if-elsif-elsif-elsif...else). In any case, I will request a company wide update to 5.10 before I propose the config file change. I trust that the given-when construct is a different animal inside than switch-case.

      I also plan to get Readonly installed to replace use constant as suggested by Conway's PBP book, unless...

      Also, thanks to the other monk who provided background on how Switch is implemented under the covers. I am not a fan of source filters.

        I propose the config file change. I trust that the given-when construct is a different animal inside than switch-case.

        It is a switch statement

        given ($convention) { when ('E') { print "east longitude $long radians\n"; } when ('W') { $long = TWOPI-$long; print "west longitude $long radians\n"; } default { print "error: $convention not valid\n"; } }

        But it doesn't have to do its own Perl parsing like Switch because it's a builtin control statement like for and while.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://811172]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found