Syntax error with Switch and prototyped-subroutines

Discussion in 'Perl Misc' started by Paul Lalli, Aug 26, 2005.

  1. Paul Lalli

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 26, 2005
    #1
    1. Advertising

  2. Paul Lalli

    John Bokma Guest

    John Bokma, Aug 26, 2005
    #2
    1. Advertising

  3. Paul Lalli

    thundergnat Guest

    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.
     
    thundergnat, Aug 26, 2005
    #3
    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. Guybrush Threepwood

    syntax error in switch statement

    Guybrush Threepwood, Jun 8, 2007, in forum: C Programming
    Replies:
    8
    Views:
    405
    Christopher Benson-Manica
    Jun 8, 2007
  2. Mark Richards
    Replies:
    3
    Views:
    346
    Tad McClellan
    Nov 18, 2007
  3. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    129
    Lasse Reichstein Nielsen
    Apr 22, 2006
  4. BrianMH

    Scope inside a prototyped function?

    BrianMH, Sep 8, 2006, in forum: Javascript
    Replies:
    1
    Views:
    95
  5. Replies:
    2
    Views:
    116
    Lasse Reichstein Nielsen
    Oct 8, 2008
Loading...

Share This Page