Strange syntax error

T

tseitlin

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.
 
A

anno4000

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:
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
 
M

Mumia W.

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:
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.
 
A

Art

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?
 
A

Art

As suspected, known bug.
http://rt.perl.org/rt3//Public/Bug/Display.html?id=34233
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?


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
 
S

Stephan Titard

Mumia W. escribió:
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
 
M

Mumia W.

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.
 
S

Stephan Titard

Mumia W. escribió:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top