Strange syntax error

Discussion in 'Perl Misc' started by tseitlin@gmail.com, Jul 11, 2006.

  1. Guest

    Hello,
    I get a syntax error I can't explain.

    The script (reduced version):

    ###############################
    use Switch;

    my $line;
    if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    $freq = $1/1000000;
    }

    #sub for opening files in 'read', 'write' or 'append' mode
    sub openFile()
    {
    my ($file, $mode) = @_;
    local *FH;
    switch($mode){
    case 'r'{
    if(!open(FH , $file)){
    print"can't open $file $!";
    return 0;
    }
    }
    case 'w'{
    if(!open(FH , ">$file")){
    print"can't open $file $!";
    return 0;
    }
    }
    case 'a'{
    if(!open(FH , ">>$file")){
    print"can't open $file $!";
    return 0;
    }
    }
    else{
    print"wrong or unsupported mode: use 'r' or 'w'";
    return 0;
    }
    }
    return *FH;
    }

    # sub simple_stats {
    # my $mean = $sum_x / $size;
    # }

    ###END########

    The error message:
    >perl -c test.pl

    String found where operator expected at test.pl line 14, near "case
    'r'"
    (Do you need to predeclare case?)
    syntax error at test.pl line 13, near "){"
    syntax error at test.pl line 19, near "}"
    test.pl had compilation errors.

    Eny of the following eliminates the error:
    1. Removing or commenting :
    if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    $freq = $1/1000000;
    }
    2. Removing or commenting the openFile sub.
    3. Removing the commented simple_stats sub !!!

    If any one has an idea, please help!
    Thanks.
     
    , Jul 11, 2006
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hello,
    > I get a syntax error I can't explain.
    >
    > The script (reduced version):
    >
    > ###############################
    > use Switch;
    >
    > my $line;
    > if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    > $freq = $1/1000000;
    > }
    >
    > #sub for opening files in 'read', 'write' or 'append' mode
    > sub openFile()
    > {
    > my ($file, $mode) = @_;
    > local *FH;
    > switch($mode){
    > case 'r'{
    > if(!open(FH , $file)){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > case 'w'{
    > if(!open(FH , ">$file")){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > case 'a'{
    > if(!open(FH , ">>$file")){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > else{
    > print"wrong or unsupported mode: use 'r' or 'w'";
    > return 0;
    > }
    > }
    > return *FH;
    > }
    >
    > # sub simple_stats {
    > # my $mean = $sum_x / $size;
    > # }
    >
    > ###END########
    >
    > The error message:
    > >perl -c test.pl

    > String found where operator expected at test.pl line 14, near "case
    > 'r'"
    > (Do you need to predeclare case?)
    > syntax error at test.pl line 13, near "){"
    > syntax error at test.pl line 19, near "}"
    > test.pl had compilation errors.
    >
    > Eny of the following eliminates the error:
    > 1. Removing or commenting :
    > if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    > $freq = $1/1000000;
    > }
    > 2. Removing or commenting the openFile sub.
    > 3. Removing the commented simple_stats sub !!!


    Well, the Switch module uses a source filter to do its thing. Strange
    things can happen with that. Switch.pm is a nice proof of concept,
    but it's rarely a real advantage over other methods. In your case, a
    simple hash table can be used to hold the information needed in the
    several branches. Untested:

    {
    my %mode_tab = (
    'r' => [ '<', 'read'],
    'w' => [ '>', 'write'],
    'a' => [ '>>', 'append to'],
    );

    sub openFile {
    my ( $file, $mode) = @_;
    warn "wrong or unsupported mode: use 'r', 'w' or 'a'" unless
    $mode_tab{ $mode};
    my ( $how, $what) = @{ $mode_tab{ $mode} };
    open my $fh, $how, $file or
    warn "Can't $what file '$file': $!";
    return $fh;
    }
    }

    Anno
     
    -berlin.de, Jul 11, 2006
    #2
    1. Advertising

  3. Mumia W. Guest

    wrote:
    > Hello,
    > I get a syntax error I can't explain.
    >
    > The script (reduced version):
    >
    > ###############################
    > use Switch;
    >
    > my $line;
    > if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    > $freq = $1/1000000;
    > }
    >
    > #sub for opening files in 'read', 'write' or 'append' mode
    > sub openFile()
    > {
    > my ($file, $mode) = @_;
    > local *FH;
    > switch($mode){
    > case 'r'{
    > if(!open(FH , $file)){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > case 'w'{
    > if(!open(FH , ">$file")){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > case 'a'{
    > if(!open(FH , ">>$file")){
    > print"can't open $file $!";
    > return 0;
    > }
    > }
    > else{
    > print"wrong or unsupported mode: use 'r' or 'w'";
    > return 0;
    > }
    > }
    > return *FH;
    > }
    >
    > # sub simple_stats {
    > # my $mean = $sum_x / $size;
    > # }
    >
    > ###END########
    >
    > The error message:
    >> perl -c test.pl

    > String found where operator expected at test.pl line 14, near "case
    > 'r'"
    > (Do you need to predeclare case?)
    > syntax error at test.pl line 13, near "){"
    > syntax error at test.pl line 19, near "}"
    > test.pl had compilation errors.
    >
    > Eny of the following eliminates the error:
    > 1. Removing or commenting :
    > if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    > $freq = $1/1000000;
    > }
    > 2. Removing or commenting the openFile sub.
    > 3. Removing the commented simple_stats sub !!!
    >
    > If any one has an idea, please help!
    > Thanks.
    >


    Every time I tried to use the Switch module, it caused
    me problems, so I don't use it any more. In particular,
    Switch seems to be incompatible with mod_perl under
    Apache.

    My theory is that Switch interacts with the perl parser
    behind the scenes, and since it's buggy, the effects of
    its modifications to the parser are weird errors.

    Don't use Switch.
     
    Mumia W., Jul 11, 2006
    #3
  4. Art Guest

    Thanks, worked perfectly.

    Just out of interest, I analyzed it a bit further, it turns out the '/'
    operator is what makes Switch to fail. In that specific case there are
    possible work-arounds: using '*0.000001' instead of '/1000000' or
    adding '#/' after the problematic line.
    I wonder if this is a known bug?


    -berlin.de wrote:
    >
    > Well, the Switch module uses a source filter to do its thing. Strange
    > things can happen with that. Switch.pm is a nice proof of concept,
    > but it's rarely a real advantage over other methods. In your case, a
    > simple hash table can be used to hold the information needed in the
    > several branches. Untested:
    >
    > {
    > my %mode_tab = (
    > 'r' => [ '<', 'read'],
    > 'w' => [ '>', 'write'],
    > 'a' => [ '>>', 'append to'],
    > );
    >
    > sub openFile {
    > my ( $file, $mode) = @_;
    > warn "wrong or unsupported mode: use 'r', 'w' or 'a'" unless
    > $mode_tab{ $mode};
    > my ( $how, $what) = @{ $mode_tab{ $mode} };
    > open my $fh, $how, $file or
    > warn "Can't $what file '$file': $!";
    > return $fh;
    > }
    > }
    >
    > Anno
     
    Art, Jul 11, 2006
    #4
  5. Art Guest

    As suspected, known bug.
    http://rt.perl.org/rt3//Public/Bug/Display.html?id=34233

    Art wrote:
    > Thanks, worked perfectly.
    >
    > Just out of interest, I analyzed it a bit further, it turns out the '/'
    > operator is what makes Switch to fail. In that specific case there are
    > possible work-arounds: using '*0.000001' instead of '/1000000' or
    > adding '#/' after the problematic line.
    > I wonder if this is a known bug?
    >
    >
    > -berlin.de wrote:
    > >
    > > Well, the Switch module uses a source filter to do its thing. Strange
    > > things can happen with that. Switch.pm is a nice proof of concept,
    > > but it's rarely a real advantage over other methods. In your case, a
    > > simple hash table can be used to hold the information needed in the
    > > several branches. Untested:
    > >
    > > {
    > > my %mode_tab = (
    > > 'r' => [ '<', 'read'],
    > > 'w' => [ '>', 'write'],
    > > 'a' => [ '>>', 'append to'],
    > > );
    > >
    > > sub openFile {
    > > my ( $file, $mode) = @_;
    > > warn "wrong or unsupported mode: use 'r', 'w' or 'a'" unless
    > > $mode_tab{ $mode};
    > > my ( $how, $what) = @{ $mode_tab{ $mode} };
    > > open my $fh, $how, $file or
    > > warn "Can't $what file '$file': $!";
    > > return $fh;
    > > }
    > > }
    > >
    > > Anno
     
    Art, Jul 12, 2006
    #5
  6. Mumia W. escribió:
    > wrote:
    >> Hello,
    >> I get a syntax error I can't explain.
    >>
    >> The script (reduced version):
    >>
    >> ###############################
    >> use Switch;
    >>
    >> my $line;
    >> if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    >> $freq = $1/1000000;
    >> }
    >>
    >> #sub for opening files in 'read', 'write' or 'append' mode
    >> sub openFile()
    >> {
    >> my ($file, $mode) = @_;
    >> local *FH;
    >> switch($mode){
    >> case 'r'{
    >> if(!open(FH , $file)){
    >> print"can't open $file $!";
    >> return 0;
    >> }
    >> }
    >> case 'w'{
    >> if(!open(FH , ">$file")){
    >> print"can't open $file $!";
    >> return 0;
    >> }
    >> }
    >> case 'a'{
    >> if(!open(FH , ">>$file")){
    >> print"can't open $file $!";
    >> return 0;
    >> }
    >> }
    >> else{
    >> print"wrong or unsupported mode: use 'r' or 'w'";
    >> return 0;
    >> }
    >> }
    >> return *FH;
    >> }
    >>
    >> # sub simple_stats {
    >> # my $mean = $sum_x / $size;
    >> # }
    >>
    >> ###END########
    >>
    >> The error message:
    >>> perl -c test.pl

    >> String found where operator expected at test.pl line 14, near "case
    >> 'r'"
    >> (Do you need to predeclare case?)
    >> syntax error at test.pl line 13, near "){"
    >> syntax error at test.pl line 19, near "}"
    >> test.pl had compilation errors.
    >>
    >> Eny of the following eliminates the error:
    >> 1. Removing or commenting :
    >> if ($line =~ /^Frequency\s+=\s+(\d+)\./){
    >> $freq = $1/1000000;
    >> }
    >> 2. Removing or commenting the openFile sub.
    >> 3. Removing the commented simple_stats sub !!!
    >>
    >> If any one has an idea, please help!
    >> Thanks.
    >>

    >
    > Every time I tried to use the Switch module, it caused
    > me problems, so I don't use it any more. In particular,
    > Switch seems to be incompatible with mod_perl under
    > Apache.
    >
    > My theory is that Switch interacts with the perl parser
    > behind the scenes, and since it's buggy, the effects of
    > its modifications to the parser are weird errors.
    >
    > Don't use Switch.
    >

    actually switch (as perl6 given) is already in 5.9.4-to-be
    so don't despair we will have it soon

    I wonder if it can be wrapped up in a stand-alone module for the
    impatient (a good question for p5p)

    hth
    --stephan
     
    Stephan Titard, Jul 12, 2006
    #6
  7. Mumia W. Guest

    On 07/12/2006 04:53 AM, Stephan Titard wrote:
    > Mumia W. escribió:
    >> [...]
    >> Don't use Switch.
    >>

    > actually switch (as perl6 given) is already in 5.9.4-to-be
    > so don't despair we will have it soon
    >
    > I wonder if it can be wrapped up in a stand-alone module for the
    > impatient (a good question for p5p)
    >
    > hth
    > --stephan


    There is already a Switch::perlish on CPAN, and I even
    wrote a switch subroutine once. However, most of the
    time, Perl's other constructs to simulate switch are
    adequate.
     
    Mumia W., Jul 12, 2006
    #7
  8. Mumia W. escribió:
    > On 07/12/2006 04:53 AM, Stephan Titard wrote:
    >> Mumia W. escribió:
    >>> [...]
    >>> Don't use Switch.
    >>>

    >> actually switch (as perl6 given) is already in 5.9.4-to-be
    >> so don't despair we will have it soon
    >>
    >> I wonder if it can be wrapped up in a stand-alone module for the
    >> impatient (a good question for p5p)
    >>
    >> hth
    >> --stephan

    >
    > There is already a Switch::perlish on CPAN, and I even wrote a switch
    > subroutine once. However, most of the time, Perl's other constructs to
    > simulate switch are adequate.
    >

    Right. My point was that once you have a switch in the core, that is the
    syntax you want to stick to, and if you ever want to use it for
    production code, it would be nice to have it backported
    or have a standalone version that works for previous versions of perl
    (quite doable actually if I followed correctly p5p on the subject)

    I don't use any switch, but I might use *given*
    hth
    --stephan
     
    Stephan Titard, Jul 12, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey Twyman
    Replies:
    8
    Views:
    578
    August Derleth
    Oct 25, 2003
  2. Qu0ll

    Strange syntax error

    Qu0ll, Apr 25, 2009, in forum: C++
    Replies:
    1
    Views:
    476
    James Kanze
    Apr 26, 2009
  3. _wolf

    strange syntax error

    _wolf, Jun 4, 2010, in forum: Python
    Replies:
    1
    Views:
    451
    Jerry Hill
    Jun 4, 2010
  4. Good Night Moon
    Replies:
    9
    Views:
    290
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    323
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page