Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Syntax error with Switch and prototyped-subroutines

Reply
Thread Tools

Syntax error with Switch and prototyped-subroutines

 
 
Paul Lalli
Guest
Posts: n/a
 
      08-26-2005
I've been pounding my head on this for almost a day, and I'm still not
seeing what I'm doing wrong. Can someone please give me a hand?

This (short-but-complete) script works as expected:

#!/usr/bin/perl
use strict;
use warnings;
use Switch;

sub foobar ($\%) {
my $foo = shift;
my $hash_ref = shift;
switch ('hello'){
case (/^[A-Z]+$/) { print "All upper\n"; }
case (/^[a-z]+$/) { print "All lower\n"; }
else { print "Mixed case\n"; }
}
}

my %temp;
foobar('world', %temp);

__END__
All lower

If, however, I switch the order of the prototyped arguments to the
subrtouine, I get a syntax error involving the switch block:

#!/usr/bin/perl
use strict;
use warnings;
use Switch;

sub foobar (\%$) {
my $hash_ref = shift;
my $foo = shift;
switch ('hello'){ # <== LINE 9
case (/^[A-Z]+$/) { print "All upper\n"; }
case (/^[a-z]+$/) { print "All lower\n"; } # <== LINE 11
else { print "Mixed case\n"; }
}
}

my %temp;
foobar(%temp, 'world');

__END__

syntax error at switchtest.pl line 9, near "){"
syntax error at line 11, near ") {"
Execution of switchtest.pl aborted due to compilation errors.

This seems to occur only when the prototype involves a \% , and that \%
appears as anything but the last element in the prototype list. If the
\% is last, the script parses correctly.

I have seen this behavior on both perl 5.8.5 for Solaris and
Activestate's perl 5.8.4 for Windows, both using Switch.pm v2.10

Can someone please point me at what I'm doing wrong? Is \% somehow
disallowed as anything but the last prototype element? (This seems
unlikely, as the script will work fine if the switch block is removed,
regardless of where the \% occurs).

Thank you,
Paul Lalli

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      08-26-2005
"Paul Lalli" <(E-Mail Removed)> wrote:

> I have seen this behavior on both perl 5.8.5 for Solaris and
> Activestate's perl 5.8.4 for Windows, both using Switch.pm v2.10


Add perl, v5.8.7 built for MSWin32-x86-multi-thread, Switch 2.10.


--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 
Reply With Quote
 
 
 
 
thundergnat
Guest
Posts: n/a
 
      08-26-2005
Paul Lalli wrote:
> I've been pounding my head on this for almost a day, and I'm still not
> seeing what I'm doing wrong. Can someone please give me a hand?
>


<snip>

> If, however, I switch the order of the prototyped arguments to the
> subrtouine, I get a syntax error involving the switch block:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Switch;
>
> sub foobar (\%$) {
> my $hash_ref = shift;
> my $foo = shift;
> switch ('hello'){ # <== LINE 9
> case (/^[A-Z]+$/) { print "All upper\n"; }
> case (/^[a-z]+$/) { print "All lower\n"; } # <== LINE 11
> else { print "Mixed case\n"; }
> }
> }
>
> my %temp;
> foobar(%temp, 'world');
>
> __END__
>
> syntax error at switchtest.pl line 9, near "){"
> syntax error at line 11, near ") {"
> Execution of switchtest.pl aborted due to compilation errors.
>
> This seems to occur only when the prototype involves a \% , and that \%
> appears as anything but the last element in the prototype list. If the
> \% is last, the script parses correctly.
>



OT: Why, oh why do you want to use prototypes in the first place? Just
pass a hash reference and be done with it.

Back on topic.

In poking around at it, it seems to boil down to a bug in the source
filtering done by the Switch module. If you deparse the script with
prototype checking disabled, the entire foobar subroutine seems to
get truncated to:

sub foobar (\%$) ;

Where exactly the bug may be, I couldn't tell you. It may not even
be due to the source filtering, but that seems to me to be most
probable.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
syntax error in switch statement Guybrush Threepwood C Programming 8 06-08-2007 12:30 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments